Data.model.updateItem is not a function TypeError: Data.model.updateItem is not a function

Data.model.updateItem is not a function TypeError: Data.model.updateItem is not a function

使用 mongo 的 keystone js 错误:Vehicle.model.updateItem 不是函数类型错误: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 是包含的对象field/fields 你想要一个新值。但它会引发错误 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!!")
        })
})

如果 itemToUpdate 具有可变数量的字段,您可以将选项添加到此调用中作为

var options = { field: 'vin_no, model_year, num_owners' }

并将其作为 Vehicle.updateItem(Vehicle.model, itemToUpdate, options, callback)

传递