1

I'm new to MEAN stack. I'm trying to implement this and this. I'm using $pull. But they ain't working maybe because my structure in mongodb is different from theirs. So let me first show you that:

enter image description here

downvoters is an string array that contains userids who downvoted that particular article. Lets say the person on downvoters[2] i.e 53et853rf later upvoted this article.Then his userid should be removed from downvoters list. Here is my code:

api.js

router.put('/update-upvotes', (req, res) => {
  let articleData = req.body;
  ...
  Article.update(
    {articleid: '5p4aqbryi'},
    { $pull: { downvoters: '53et853rf' } }
  );
  Article.findOneAndUpdate(
    {articleid: '5p4aqbryi'},
    {upvotes: articleData.upvotes, upvoters: articleData.upvoters}, useFindAndModify=false,
    (error, user) => {
      if(error) {
        ...
      }
      else {
        ...
      }
    })
   })

But that id is not deleted. There's no error or warning on console. Please correct me.

And here is the schema

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const articleSchema = new Schema({
  articleid: String,
  title: String,
  content: String,
  date: String,
  contributor: String,
  upvotes: Number,
  upvoters: [],
  downvotes: Number,
  downvoters: []
})
module.exports = mongoose.model('article', articleSchema, 'articles');

PS: Let articleId and downvoter id be hardcoded now. I'll make them dynamic later.

2
  • add a .catch(e=>console.log(e)) at the end for error logging. Commented Dec 26, 2019 at 20:32
  • Added. But still no error or warning. It is just not doing my work. Commented Dec 26, 2019 at 20:35

1 Answer 1

1

Both upvoters and downvoters are String arrays so your Mongoose schema should look like below:

const articleSchema = new Schema({
    articleid: String,
    title: String,
    content: String,
    date: String,
    contributor: String,
    upvotes: Number,
    upvoters: [String],
    downvotes: Number,
    downvoters: [String]
});

You should also keep in mind that update() is an asynchronous operation which needs to be awaited or handled as Promise so:

let opResult = await Article.update(
    {articleid: '5p4aqbryi'},
    { $pull: { downvoters: '53et853rf' } }
);

or

Article.update(
        { articleid: '5p4aqbryi' },
        { $pull: { downvoters: '53et853rf' } }
    ).then(opResult => console.log(opResult));
Sign up to request clarification or add additional context in comments.

10 Comments

I appreciate your help. But the first solution is giving this error: SyntaxError: await is only valid in async function. And the second one had no change at all, nothing happened.
You need to mark your function as async, like: async () => { await Article.update(...) } . Besides that I've tested your example and it works so please make sure that you're running your query on the collection where your data resides. Try to make sure that Article.findOne({ articleId: '5p4aqbryi' }) returns the document you're trying to modify.
And please make sure that you're "awaiting" the execution of first function. Otherwise update and findOneAndUpdate may interfere with each other. If you can't use awaits run findOneAndUpdate in a callback of update()
@Tanzeel lesson learned - update returns Promise which needs to be await-ed or the continuation should be run as .then() otherwise the operation won't get executed
I'll always remember this. :-). By the way. You an see this: fast-depths-77615.herokuapp.com/options. this is what I'm making.
|

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.