N
N
Nori Nyah2019-11-14 18:54:29
JavaScript
Nori Nyah, 2019-11-14 18:54:29

Why doesn't mongoose update the document?

User.findOne({ userId: userid }, function (err, user) {
            if (err) return console.log('ERROR');

            user.lvl += 1;
            user.save((err, doc) => {
                if (err) return console.log('save error!');

                console.log('Saved user: ', doc);
            });
        });

Why doesn't this code update the document? There are no errors, a supposedly saved copy is returned. In fact, nothing has been updated.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
grinat, 2019-11-14
@grinat

For example, this lvl is not in the scheme.

H
hzzzzl, 2019-11-19
@hzzzzl

const updatedUser = await User.findOneAndUpdate(
  { userId: userid }, 
  { $inc: { lvl: 1 } },
  { new: true }
)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question