gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 09:58:50 25 4
gpt4 key购买 nike

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

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);
});
},

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

最佳答案

给你:

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);
})
}

基本上只是先获取库存记录,然后您可以在Order 查询中使用它。我还将 Inventory.find 换成了 .findOne(),因为看起来这就是您打算使用它的方式。

关于node.js - 我想将结果数据存储在一个变量中,以便在 sails js (mongodb) 中进一步使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38897467/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com