2

This is my schema:

const productSchema = new mongoose.Schema({
  name: String,
  imageUrl: String,
  category: String,
  price: Number,
  description: String,
  featured: {
    default: false
  },
  rating: [
    {
      userName: String,
      score: Number,
      comment: String
    }
  ]
});

And this is how I was trying to push data into my database.

app.route("/review").post(function(req, res) {
  const score = req.body.score;
  const comment = req.body.comment;

  if (req.isAuthenticated()) {
    const review = {
      $push: {
        rating: {
          userName: req.user.fName,
          score: score,
          comment: comment
        }
      }
    };
    console.log(review, req.body.productName);
    Product.updateOne({ name: req.body.productName }, review, function(
      err,
      done
    ) {
      if (err) {
        console.log(err);
      } else {
        res.redirect("/products");
      }
    });
  } else {
    res.redirect("/login");
  }
});

In the official documentation, it says that this is the way to push data in the MongoDB array. But still having no luck. Help me to push reviews in the rating array field. Thank You.

4
  • Any errors or logs you want to share? Commented Mar 7, 2020 at 10:15
  • It doesn't show any error, it goes to the else section & redirects to "/products". Commented Mar 7, 2020 at 12:52
  • what is this userName: req.user.fName ? it has to be from req.body !! Commented Mar 7, 2020 at 18:58
  • no, it's the name of the logged in user Commented Mar 8, 2020 at 11:22

1 Answer 1

1

for my answer im using mongoose function findOneAndUpdate you can use updateOne instead , The writing to the database will be in a async function in order to not block the node process (will be alot faster). Puting all the code in a try-catch block will allow you to control any errors if accrued. You can read more about mongoose driver function and findOneAndUpdate here: https://docs.mongodb.com/manual/reference/method/db.collection.findOneAndUpdate/ more about async function: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function

app.route('/review').post(async function(req, res) {
//destructuring values from body-parser
  const { score, comment, productName } = req.body;

  if (req.isAuthenticated()) {
    //creating the rating obj
    const rating = {
      userName: req.user.fName,
      score: score,
      comment: comment
    };
    console.log(review, productName);
   //setting it all in a try-catch block
    try {
      const product = await Product.findOneAndUpdate(
        {name: productName},
        { $set: { rating: rating } },
        { new: true }
      ).then(err => {
        if (err) throw err;
        return res.redirect('/products');
      });
      res.redirect('/login');
    } catch (error) {
      console.log(error);
    }
  }
});

Hopefully this makes sense, good luck!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.