node.js - Sails v1 不再支持模型实例方法

标签 node.js sails.js

我不断收到错误消息“在模型 usercustomToJSON 属性中: 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/

相关文章:

javascript - V8 Javascript 引擎:v8::参数和函数模板

javascript - 通过针发送 JSON 对象

javascript - 有没有办法扩展 sails.js 中的 Controller

javascript - 在 sails.js 中区分移动版本和桌面版本的最简单方法是什么

json - 使用 NodeJS 从 json 数组中查找项目

node.js - Express/node 服务器随机崩溃,events.js ECONNRESET

javascript - 如何在 promise 中履行 promise

javascript - 使用 .filter 搜索数组

html - 在使用 Node.js 创建的网站上播放 mp3

javascript - 在我的服务器上运行 Sails.js 应用程序几分钟后无法访问 MongoDB