当我在环回之前定义方法之前,PUT被破坏了

时间:2015-03-19 16:13:29

标签: post put loopbackjs

我在创建/更新环回数据时尝试更改某些字段。 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();
  });
};

我收到此错误消息:

{   "错误":{     " name":" TypeError",     "状态":500,     "消息":"无法读取属性' name'未定义",     " stack":" TypeError:无法读取属性' name'未定义的\ n在/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)"   } }

1 个答案:

答案 0 :(得分:0)

在部分更新可能的多个模型时,

ctx.instance未定义,而是访问ctx.data属性。

检查docs