我想将结果数据存储在一个变量中,以便在sails js(mongodb)中进一步使用

Ahs*_*eel 2 mongodb node.js npm sails.js

我想从数据库中获取价格以存储订单模型,这是我的代码

newOrder: function(req,res){
    var data = req.body;
    const orderNo = data.orderNo;
    const user = data.user;
    const business = data.business;
    const inventory = data.inventory;
    const price = Inventory.find({id: data.inventory}).exec(function(err,record){ return record.price});
    const address = data.address;
    const quantity = data.quantity;
    const contactNumber = data.contactNumber;

    Order.create({
      orderNo: orderNo,
      user: user,
      business: business,
      inventory: inventory,
      price: price,
      address: address,
      quantity: quantity,
      contactNumber: contactNumber
      }).then(function(result){
      res.ok(result);
    });
  },
Run Code Online (Sandbox Code Playgroud)

我知道这是不正确的,但我不知道该怎么做,内部查询没有返回任何东西,我想将结果数据存储在变量中以供进一步使用.请帮忙

Fis*_*sio 5

干得好:

newOrder: function(req,res){
    var data = req.body;
    const orderNo = data.orderNo;
    const user = data.user;
    const business = data.business;
    const inventory = data.inventory;
    const address = data.address;
    const quantity = data.quantity;
    const contactNumber = data.contactNumber;

    Inventory.findOne({id: data.inventory})
    .then(function(record) {
        // Price is available here as record.price,
        // do whatever you want with it
        return Order.create({
            orderNo: orderNo,
            user: user,
            business: business,
            inventory: inventory,
            price: record.price,
            address: address,
            quantity: quantity,
            contactNumber: contactNumber
        });
    })
    .then(function(createdOrder) {
        res.ok(createdOrder);
    })
    .catch(function(err) {
        console.log("Error ar newOrder:", err);
        return res.serverError(err);
    })
}
Run Code Online (Sandbox Code Playgroud)

基本上只是首先获取库存记录,然后您可以在Order查询中使用它.我也交换了Inventory.find,.findOne()因为它似乎是你打算如何使用它.