【问题标题】:Sailjs-Waterline: Serialize, Parse and initialize an ModelSails Js-Waterline:序列化、解析和初始化模型
【发布时间】:2015-05-30 06:05:44
【问题描述】:

假设我使用下一个命令找到了用户:

User.findOne(id).exec(function(err, user){
  redis.set(JSON.stringify(user), id);
})

之后我从 redis 加载我的对象

redis.get(id, function(err, reply){
  if(!err && reply) {
    var user = JSON.parse(reply);

    // here methods like user.save() or any of defined manually by developer is unavailable

    // 
  } else {
    ..
  }
})

用户模型示例:

module.exports = {

  attributes    : {
    // Simple attribute:
    // name: 'STRING',

    // Or for more flexibility:
    // phoneNumber: {
    //  type: 'STRING',
    //  defaultValue: '555-555-5555'
    // }

    email : {
      type: 'string',
      unique: true
    },

    // ... 

    verifyPass: function(pass, callback) {
      var obj = this.toObject();
      if (callback) {
        return Password.compare(pass, obj.local.password, callback);
      }
      return Password.compareSync(pass, obj.local.password);
    },

    // retrieve profile for sending to front end
    getProfile: function() {
      var obj = this.toObject();
      var profile = {};

      profile.id = obj.id;
      // ...
      return profile;
    },

每当我从 json 解析水线模型时,我都需要所有这些方法。有没有办法在不触发 db 的情况下对其进行初始化。如果我可以调用 user.save() 也很好。

【问题讨论】:

    标签: json node.js performance sails.js waterline


    【解决方案1】:

    遗憾的是,目前没有记录在案的公共 API,但您可以使用,

    var user = new PersonCollection._model(values, {showJoins: true});
    

    看看这对你有什么作用!

    【讨论】:

      猜你喜欢
      • 2014-07-18
      • 1970-01-01
      • 2012-09-24
      • 2017-01-01
      • 2015-08-16
      • 2011-03-26
      • 1970-01-01
      • 1970-01-01
      • 2015-11-21
      相关资源
      最近更新 更多