我如何访问车辆模型查询的res结果,然后将res值分配给data.diff {after},我尝试分配它,但是在车辆功能为空的情况下访问了它。刚刚接触node,谢谢。
schema.methods.log = function (data) {
let condition = { VIN: this.VIN }
let Vehicle = keystone.list('Vehicle');
data.diff = {
}
Vehicle.model.findOne(condition).then(function (res) {
console.log("res" , res)
}).catch(function (err) {
console.log(err)
})
console.log("res", res)
data.diff = {
before: this,
after: res,
}
return LogSchema.create(data)
}
}
答案 0 :(得分:-1)
最好使用async/await
。因此,首先检索res
,然后分配res
。
请检查以下代码。
schema.methods.log = async function (data) {
let condition = { VIN: this.VIN }
let Vehicle = keystone.list('Vehicle');
data.diff = {};
const res = await Vehicle.model.findOne(condition);
console.log("res", res);
data.diff = {
before: this,
after: res,
};
return LogSchema.create(data)
}