Sequelize, convert entity to plain object

后端 未结 9 1112
说谎
说谎 2020-12-12 16:53

I\'m not very familiar with javascript, and stunning, because i can\'t add new property, to object, that fetched from database using ORM names Sequelize.js.

To avoid

9条回答
  •  醉梦人生
    2020-12-12 17:29

    If I get you right, you want to add the sensors collection to the node. If you have a mapping between both models you can either use the include functionality explained here or the values getter defined on every instance. You can find the docs for that here.

    The latter can be used like this:

    db.Sensors.findAll({
      where: {
        nodeid: node.nodeid
      }
    }).success(function (sensors) {
      var nodedata = node.values;
    
      nodedata.sensors = sensors.map(function(sensor){ return sensor.values });
      // or
      nodedata.sensors = sensors.map(function(sensor){ return sensor.toJSON() });
    
      nodesensors.push(nodedata);
      response.json(nodesensors);
    });
    

    There is chance that nodedata.sensors = sensors could work as well.

提交回复
热议问题