0

enter image description here

how do i delete a productid say 5bbee0b7e5fcb61df834f7d6 from this arrat have tried

router.post('/empty-cart', isLoggedIn, function(req, res, next) {
  console.log("user" + req.body.user + ",product" + req.body.productId);
  slug = req.body.productId;
  user = req.body.user;
  User.findOne({ _id: user }, {
    $pull: { productId: slug }
  }, function(err, model) {})

  console.log(slug);
  meanlogger.log('trash', 'Emptied cart', req.user);

  res.redirect('/shopping-cart');
});

but seems like it wont apply for arrays as it considers only 1st element looking for suggestions

1

2 Answers 2

1

You cannot use $pull with findOne you should to use update

router.post('/empty-cart', isLoggedIn, function(req, res, next) {
  console.log("user" + req.body.user + ",product" + req.body.productId);
  slug = req.body.productId;
  user = req.body.user;
  User.update({ _id: user }, { $pull: { productId: slug } }, function(err, model) {
    console.log(slug);
    meanlogger.log('trash', 'Emptied cart', req.user);
    res.redirect('/shopping-cart');
  })
});

this is the correct way.

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

3 Comments

thanks for response my query is i have an array productid and im doing nothing to loop through that array so cant achieve the requirement
have you tried with update? if so, you will achieve your requirement. Query is tested and working fine, may be you have issue with user. Try to print and check the query.
yes i have been using update only now someone suggested to use findone let me test with user as you suggest
0

There is another way. You should find document first, then pull by ObjectId.

router.post('/empty-cart', isLoggedIn, function (req, res, next) {
    console.log('user' + req.body.user + ',product' + req.body.productId);
    const slug = mongoose.Types.ObjectId(req.body.productId);
    const user = req.body.user;
    User.findOne({ _id: user }, function (err, model) {
        if (err) return res.redirect('/error-page');
        if (!model) return res.redirect('/notfound-page');
        model.productId.pull(slug);
        model.save(function (err) {
          if (err) return res.redirect('/error-page');
          console.log(slug);
          meanlogger.log('trash', 'Emptied cart', req.user);
          res.redirect('/shopping-cart');
        });
    });
});

3 Comments

hey Enxtur thanks for the answer seemed very much right but gave an error TypeError: Cannot read property 'productId' of null for this line model.productId.pull(slug);
In your case, user not found by your input as req.body.user. Check your data dude :) and I edit my post for checking errors and model
yeah you are right that solved the issue i was retreving product id both end which led to error thanks for the help

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.