MongoDB更新字段数据

时间:2018-06-21 06:48:00

标签: node.js mongodb reactjs express mongodb-query

如何使用db.findAndUpdate表用户并将余额从以前的金额更改为较新的金额? 我已经尝试获得平衡并且结果什么也没有显示,现在我混淆了在findAndUpdate中编写。这是我的代码:

api:

app.post('/api/account/transfer', (req, res, next) => {
    const { body } = req;
    const {
      sender,
      receiver,
      amount,
      user,
      balance,
    } = body;


    if (!sender) {
      return res.send({
        success: false,
        message: 'Error: Sender cannot be blank!'
      });
    }
    if (!receiver) {
      return res.send({
        success: false,
        message: 'Error: Receiver cannot be blank!'
      });
    }
    if (!amount) {
      return res.send({
        success: false,
        message: 'Error: Fill Amount!'
      });
    } else if(balance < amount || balance == 0) {
      return res.send({
        success: false,
        message: 'Insufficient funds!'
      });
    }

    //save the new transaction
    const newTransaction = new Transaction();

    newTransaction.sender = sender;
    newTransaction.receiver = receiver;
    newTransaction.amount = amount;

    newTransaction.save( (err, transaction) => {
      if(err) {
        return res.send({
          success: false,
          message: 'Error: Server error.'
        });
      }else{
        return res.send({
          success: true,
          message: 'Transfer Success!'
        });
      }
    });

    //update user balance
    User.findOneAndUpdate({
    });

,这是屏幕截图: transaction

所以我想要的是,如果我作为接收者,我的余额将会增加。

1 个答案:

答案 0 :(得分:0)

尝试此操作,findOneAndUpdate操作不会等待保存操作完成。

app.post('/api/account/transfer', (req, res, next) => {
    const {
        body
    } = req;

    const fields = ['sender', 'receiver', 'amount', 'balance'];
    fields.forEach((field) => {
        if (!body[field]) {
            return res.send({
                success: false,
                message: 'Error: ' + field + ' cannot be blank!'
            });
        }
    })
    if (body.balance < body.amount || body.balance == 0) {
        return res.send({
            success: false,
            message: 'Insufficient funds!'
        });
    }

    //save the new transaction
    let newTransaction = new Transaction();
    // newTransaction = Object.assign(newTransaction, body); // If you want to copy all params from body to newTransaction 
    newTransaction.sender = body.sender;
    newTransaction.receiver = body.receiver;
    newTransaction.amount = body.amount;

    newTransaction.save((err, transaction) => {
        if (err) {
            return res.send({
                success: false,
                message: 'Error: Server error.'
            });
        } else {
            //update user balance
            User.findOneAndUpdate({
                /*query*/
            }, {
                /*body*/
            }, (err, data) => {
                if (err)
                    return res.send({
                        success: false,
                        message: 'Error: Server error.'
                    });
                return res.send({
                    success: true,
                    message: 'Transfer Success!'
                });
            });
        }
    });
});