MongoDB
 sql >> Database >  >> NoSQL >> MongoDB

Promessa concatenata di aggiornare un documento di riferimento in Mongoose

Mongoose gestisce le promesse:http://mongoosejs.com/docs/promises.html

Quindi puoi semplicemente :

app.post('/accounts', authenticate, (req, res) => {
    var account = new Account({
        name: req.body.name,
        _owner: req.body._owner,
    });
    account.save()
        .then((doc) => User.findOneAndUpdate(
            { uid: req.body._owner },
            { $set: { account: doc._id } },
            { new: true }
        )
        .then(() => doc);
    }).then((account) => {
        res.send(account);
    }, (e) => {
        res.status(400).send(e);
    });
});