Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js Sails v1中不再支持模型实例方法_Node.js_Sails.js - Fatal编程技术网

Node.js Sails v1中不再支持模型实例方法

Node.js Sails v1中不再支持模型实例方法,node.js,sails.js,Node.js,Sails.js,我一直在modeluser的customToJSON属性中得到错误“: Sails v1中不再支持模型实例方法。 请将此实例方法中的逻辑重构为 静态方法、模型方法或助手。“。我是node.js新手,不知道该怎么办 My User.js const bcrypt = require("bcrypt") const Promise = require("bluebird") module.exports = { attributes: { firstName: { type

我一直在model
user
customToJSON
属性中得到错误“: Sails v1中不再支持模型实例方法。 请将此实例方法中的逻辑重构为 静态方法、模型方法或助手。“。我是node.js新手,不知道该怎么办

My 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的更多信息,请参阅;有关替换实例方法的更多信息,请参阅