【问题标题】:PUT is broken when I define method before save, in loopback当我在保存之前在环回中定义方法时,PUT 被破坏
【发布时间】:2015-03-19 16:13:29
【问题描述】:

当我使用环回创建/更新数据时,我尝试更改某些字段。 POST 方法有效,但现在 PUT 方法已关闭。

我的 category.json:

{
 "name": "category",
 "base": "PersistedModel",
 "properties": {
   "name": {
     "type": "string",
     "required": true
   },
   "image": {
     "type": "string",
     "required": true
   }
 },
 "validations": [],
 "relations": {},
 "acls": [],
 "methods": []
}

以及用于定义方法的 category.js:

module.exports = function(categorie) {

  categorie.observe('before save', function(ctx, next){

    ctx.instance.name = ctx.instance.name.toUpperCase().trim();
    ctx.instance.image = ctx.instance.image.trim();
    ctx.instance.image = ctx.instance.image.charAt(0).toUpperCase() + ctx.instance.image.slice(1);

    next();
  });
};

我收到此错误消息:

{ “错误”: { “名称”:“类型错误”, “状态”:500, "message": "无法读取未定义的属性 'name'", "stack": "TypeError: Cannot read property 'name' of undefined\n at /home/pitt/warm-wildwood-2324/common/models/category.js:5:35\n at notifySingleObserver (/home/pitt/ warm-wildwood-2324/node_modules/loopback-datasource-juggler/lib/model.js:600:22)\n (...) 在 Function.ModelBaseClass.notifyObserversOf (/home/pitt/warm-wildwood-2324/node_modules /loopback-datasource-juggler/lib/model.js:593:8)" } }

【问题讨论】:

    标签: post put loopbackjs


    【解决方案1】:

    ctx.instance 不是未定义的,可能是多个模型的部分更新,而是访问ctx.data 属性。

    查看docs

    【讨论】:

      猜你喜欢
      • 2021-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多