【问题标题】:Unable to delete _id field from object after create using feathersjs使用 feathersjs 创建后无法从对象中删除 _id 字段
【发布时间】:2018-03-07 13:17:34
【问题描述】:

我想在插入数据后在我的节点应用程序中修改我的 hook.data 对象。其实我做不到。

create: [function(hook, next) {
      delete hook.data._id;
      hook.data = { problem: hook.data }
      postJson(hook.app.get('jsprintUrl'), hook.data)
        .then(data =>{
          hook.result = data;
          next()
        })
    }]

结果: _id 仍然存在

{
"_id": "59ca334e7bc4e06b140aadf9",
    "algorithm": [
        {
            "name": "SA"
        }
    ] 
}

【问题讨论】:

    标签: javascript node.js object mongoose feathersjs


    【解决方案1】:

    我以下列方式使用 hook.result 更新对象,并且 hook.result 会将其 Mongoose 文档转换为普通对象。更多信息reference link

    create: [function(hook, next) {
          delete hook.result._id;
          hook.result = { problem: hook.result }
          postJson(hook.app.get('jsprintUrl'), hook.result)
            .then(data =>{
              hook.result = data;
              next()
            })
        }]
    

    结果:已从响应中删除

    {
      "algorithm": [
            {
                "name": "SA"
            }
        ]
    }
    

    【讨论】:

      猜你喜欢
      • 2015-06-07
      • 2018-09-28
      • 2017-06-10
      • 2018-06-01
      • 1970-01-01
      • 2013-07-07
      • 1970-01-01
      • 1970-01-01
      • 2021-11-24
      相关资源
      最近更新 更多