我有一个名为question的模型,我在其中定义了beforeCreate和beforeUpdate方法来设置createdAt和updatedAt timestamps.But似乎从未在我使用api explorer创建新问题时调用。
这是我的问题.json:
{
"name": "question",
"plural": "questions",
"base": "PersistedModel",
"idInjection": true,
"properties": {
"title": {
"type": "string",
"required": true
},
"body": {
"type": "string",
"required": true
},
"views": {
"type": "number"
},
"createdAt": {
"type": "date",
"required": true
},
"updatedAt": {
"type": "date",
"required": true
},
"id": {
"type": "number"
}
},
"validations": [],
"relations": {
"tags": {
"type": "hasMany",
"model": "tag",
"foreignKey": "question_id"
}
},
"acls": [],
"methods": []
}
和question.js
module.exports = function(question) {
question.beforeCreate = function (next, modelInstance) {
modelInstance.createdAt = Date.now();
modelInstance.updatedAt = Date.now();
next();
};
question.beforeUpdate = function (next, modelInstance) {
modelInstance.updatedAt = Date.now();
next();
};
};
创建时遇到的错误是:
{
"error": {
"name": "ValidationError",
"status": 422,
"message": "The `question` instance is not valid. Details: `createdAt` can't be blank (value: undefined); `updatedAt` can't be blank (value: undefined).",
"statusCode": 422,
"details": {
"context": "question",
"codes": {
"createdAt": [
"presence"
],
"updatedAt": [
"presence"
]
},
"messages": {
"createdAt": [
"can't be blank"
],
"updatedAt": [
"can't be blank"
]
}
},
"stack": "ValidationError: The `question` instance is not valid. Details: `createdAt` can't be blank (value: undefined); `updatedAt` can't be blank (value: undefined).\n at /Users/dev/Desktop/workspace/stackclone/node_modules/loopback-datasource-juggler/lib/dao.js:252:12\n at ModelConstructor.<anonymous> (/Users/dev/Desktop/workspace/stackclone/node_modules/loopback-datasource-juggler/lib/validations.js:460:11)\n at ModelConstructor.next (/Users/dev/Desktop/workspace/stackclone/node_modules/loopback-datasource-juggler/lib/hooks.js:75:12)\n at ModelConstructor.<anonymous> (/Users/dev/Desktop/workspace/stackclone/node_modules/loopback-datasource-juggler/lib/validations.js:457:23)\n at ModelConstructor.trigger (/Users/dev/Desktop/workspace/stackclone/node_modules/loopback-datasource-juggler/lib/hooks.js:65:12)\n at ModelConstructor.Validatable.isValid (/Users/dev/Desktop/workspace/stackclone/node_modules/loopback-datasource-juggler/lib/validations.js:433:8)\n at /Users/dev/Desktop/workspace/stackclone/node_modules/loopback-datasource-juggler/lib/dao.js:248:9\n at doNotify (/Users/dev/Desktop/workspace/stackclone/node_modules/loopback-datasource-juggler/lib/model.js:641:49)\n at doNotify (/Users/dev/Desktop/workspace/stackclone/node_modules/loopback-datasource-juggler/lib/model.js:641:49)\n at doNotify (/Users/dev/Desktop/workspace/stackclone/node_modules/loopback-datasource-juggler/lib/model.js:641:49)"
}
}
我的意见是:
{
"title": "title",
"body": "body",
"views": 0
}
提前致谢
答案 0 :(得分:3)
模型挂钩已被弃用,转而使用操作挂钩:
http://docs.strongloop.com/display/public/LB/Operation+hooks
在您的情况下,移植到操作挂钩非常小:
module.exports = function(Question) {
Question.observe('before save', function(context, next) {
context.instance.createdAt = new Date(2014,2,2);
context.instance.updatedAt = Date.now();
next();
});
};
答案 1 :(得分:1)
您可以在question.js
module.exports = function(Question) {
Question.beforeRemote('create', function(context, user, next) {
var req = context.req;
req.body.createdAt = new Date(2014,2,2);
req.body.updatedAt = Date.now();
next();
});
};