我不断收到错误消息“在模型 user
的 customToJSON
属性中:
Sails v1 中不再支持模型实例方法。
请将此实例方法的逻辑重构为
静态方法、模型方法或帮助程序。”。我是 node.js 的新手,不知道该怎么做。
我的 User.js
const bcrypt = require("bcrypt")
const Promise = require("bluebird")
module.exports = {
attributes: {
firstName: {
type: "string",
required: true
},
lastName: {
type: "string",
required: true
},
username: {
type: "string",
required: true,
unique: true
},
email: {
type: "email",
required: true,
unique: true
},
password:{
type: "string",
minLength: 6,
required: true,
columnName: "encryptedPassword"
},
customToJSON: function(){
const obj = this.toObject()
delete obj.password
}
}
};
最佳答案
首先,我希望您喜欢使用 Node 和 Sails 进行开发。
现在要解决您的问题,我猜您使用的是基于 sails v0.12 的指南或教程,但如错误消息所述,自 v1 起,实例方法已从 Sails 和 Waterline 中删除。
话虽如此,解决方案非常简单,将您的 customToJSON 方法移出模型的属性部分。这基本上允许 sails v1 找到它。
所以你的模型看起来像这样
...
attributes: {
...
,
password:{
type: "string",
minLength: 6,
required: true,
columnName: "encryptedPassword"
},
},
customToJSON: function() {
return _.omit(this, ['password']);
},
...
有关 sails 中 customToJSON 的更多信息,请参阅 here有关替换实例方法的更多信息,请参阅 here .
关于node.js - Sails v1 不再支持模型实例方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54842976/