【问题标题】:Data.model.updateItem is not a function TypeError: Data.model.updateItem is not a functionData.model.updateItem 不是函数 TypeError:Data.model.updateItem 不是函数
【发布时间】:2019-12-20 16:30:35
【问题描述】:

使用 mongo 的 keystone js 出错:Vehicle.model.updateItem 不是函数 TypeError: Vehicle.model.updateItem 不是函数。

目标是使用对象更新模型,就像我使用 ff.代码如下。

CreateItems - Vehicle 是模型。而 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!!")
          })

知道如何使用对象更新数据。 ?

【问题讨论】:

    标签: javascript node.js mongodb mongoose keystonejs


    【解决方案1】:

    你应该这样使用它

    // 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)

    【讨论】:

    • 假设vin_no, model_year, num_owners 是您要从可能有其他字段的itemToUpdate 对象中更新的字段。
    • 我想在更新数据时使用的那些值怎么样?
    • 你必须循环并重复,你也可以使用async.forEach
    猜你喜欢
    • 2017-05-06
    • 1970-01-01
    • 2021-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-21
    • 1970-01-01
    • 2018-01-22
    相关资源
    最近更新 更多