【问题标题】:Sails model query风帆型号查询
【发布时间】:2015-01-28 07:56:30
【问题描述】:

我有 3 个模型(房间、模块和设备): 房间:

/**
 * Room.js
 *
 * @description :: TODO: You might write a short summary of how this model works and what it represents here.
 * @docs        :: http://sailsjs.org/#!documentation/models
 */

module.exports = {

  attributes   : {
    name    : {
      type     : 'string',
      required : true
    },
    image   : {
      type : 'binary'
    },
    modules : {
      collection : 'Module',
      via        : 'inRoom'
    },
    toJSON  : function ()
    {
      var obj = this.toObject();
      if (obj.image)
      {
        var base64data = new Buffer(obj.image.toString(), 'binary').toString();
        obj.image = base64data;
      }
      return obj;
    }
  },
  beforeCreate : function (attrs, next)
  {
    next();
  }
  ,
  beforeUpdate : function (attrs, next)
  {
    next();
  }
}
;

模块:

/**
 * Module.js
 *
 * @description :: TODO: You might write a short summary of how this model works and what it represents here.
 * @docs        :: http://sailsjs.org/#!documentation/models
 */

module.exports = {

  attributes : {
    name      : {
      type     : 'string',
      required : true
    },
    nbDevices : {
      type       : 'integer',
      defaultsTo : 1
    },
    image     : {
      type : 'binary'
    },
    inRoom    : {
      model : 'Room'
    },
    devices   : {
      collection : 'Device',
      via        : 'module'
    }
  }
};

设备:

/**
 * Device.js
 *
 * @description :: TODO: You might write a short summary of how this model works and what it represents here.
 * @docs        :: http://sailsjs.org/#!documentation/models
 */

module.exports = {

  attributes : {
    name   : {
      type     : 'string',
      required : true
    },
    index  : {
      type       : 'integer',
      defaultsTo : 0
    },
    value  : {
      type       : 'integer',
      defaultsTo : 0
    },
    image  : {
      type : 'binary'
    },
    module : {
      model : 'Module'
    }
  }
};

我想检索包含所有设备的所有房间。现在我喜欢这样:

Room.find().populate('modules')
      .exec(function (err, rooms)
      {
        var index = 0;
        var total = rooms.length-1;
        _(rooms).forEach(function (room)
        {
          Device.find({module : _.pluck(room.modules, 'id')}).populate("module").exec(function (err, data)
          {
            room.devices = data;
            console.log(room);
            if(total == index)
            {
              return res.json(rooms);

            }
            index++;
          });
        }).value();

      });

但它看起来并不干净/安全。还有另一种方法可以实现这一目标吗? 我看到这篇帖子Sails.js populate nested associations,但无法使用 find 而不是 findOne。

【问题讨论】:

    标签: node.js mongodb model sails.js waterline


    【解决方案1】:

    您可以使用async 或其他用于控制流的库添加到您在回答中提到的example 以执行以下操作:

    var async = require('async');
    
    Room.find()
    .populate('modules')
    .exec(function (err, rooms) {
        // if(err) ...
    
        async.forEach(room,
            // apply on each room
            function(room, cb){
                if(err || !room) return cb(err);
    
                Device.find({module : _.pluck(room.modules, 'id')})
                .populate("module")
                .exec(function(err, devices){
                    room.devices = devices;
                    cb();
                });
    
            },
            // when all rooms are done
            function(err){
                // if(err) ...
                res.json(rooms);
            }
        );
    });
    

    【讨论】:

    • 完美运行!非常感谢 !这样更干净:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-17
    • 2017-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多