使用 mongo 的 keystone js 中出现错误:Vehicle.model.updateItem 不是函数 TypeError: Vehicle.model.updateItem 不是函数。
目标是使用对象更新模型,就像我使用 ff 创建项目的方式一样。代码如下。
CreateItems - 车辆是模型。 postJson 是 json 数组对象。
keystone.createItems({
Vehicle: postJson
}, function (err, stats) {
if (err) return res.json(err);
res.json({
data: stats.message
});
console.log("Succeeded!!")
});
现在我尝试使用 json 对象数组和唯一 id 来更新模型上的数据,其中 itemToUpdate 是匹配我要更新的文档的查询,fieldToUpdate 是包含您想要新值的字段的对象为了。但它会引发错误 Vehicle.model.updateItem is not a function TypeError: Vehicle.model.updateItem is not a function
更新代码
var itemToUpdate = {
vin_no: value.vin_no
}
var fieldToUpdate = {
Vehicle: value
}
Vehicle.model.updateItem(
itemToUpdate,
fieldToUpdate,
function (err, stats) {
if (err) return res.json(err);
res.json({
data: stats.message
});
console.log("Succeeded!!")
})
知道如何使用对象更新数据。 ?
你应该像这样使用它
// assuming value is object with all the fields.
var itemToUpdate = {
vin_no: value.vin_no
}
Vehile.model.findOne(itemToUpdate, function(error, vehicleObject) {
Vehicle.updateItem(
vehicleObject,
value,
function (err) {
// err can be Error object or an object with 'error' and/or 'detail' property
if (err) return res.json(err);
res.json({
status: "success"
});
console.log("Succeeded!!")
})
})
if itemToUpdate
具有可变数量的字段,您可以向此调用添加选项,如下所示
var options = { field: 'vin_no, model_year, num_owners' }
并将其传递为Vehicle.updateItem(Vehicle.model, itemToUpdate, options, callback)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)