在环回中使用 node.js 引用模型上的 属性

Referencing a property on a model with node.js in Loopback

尝试在环回中引用 属性 并考虑我尝试这样做的时间,我显然遗漏了一些基本概念。

非常简单,我有一个问题模型,它有一个整数 属性 点,我想做的只是将点 属性 打印到控制台。

module.exports = function(Question) {
    Question.observe('after save', function(ctx, next) {
    console.log(Question.prototype.points)
    next();
  });
};

当我执行上述操作时,它会打印出 undefined

考虑到这是一个如此简单的操作,我错过了什么?

json 文件:

{
  "name": "Question",
  "plural": "Questions",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "text": {
      "type": "string",
      "required": true
    },
    "points": {
      "type": "number",
      "required": true
    }
  },
}

你快到了。使用保存后获得的上下文对象。

module.exports = function(Question) {
  Question.observe('after save', function(ctx, next) {
    console.log(ctx.instance.points);
    next();
  });
};

Operation hooks documentation.