Sequelize, convert entity to plain object

后端 未结 9 1196
说谎
说谎 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:39

    As CharlesA notes in his answer, .values() is technically deprecated, though this fact isn't explicitly noted in the docs. If you don't want to use { raw: true } in the query, the preferred approach is to call .get() on the results.

    .get(), however, is a method of an instance, not of an array. As noted in the linked issue above, Sequelize returns native arrays of instance objects (and the maintainers don't plan on changing that), so you have to iterate through the array yourself:

    db.Sensors.findAll({
        where: {
            nodeid: node.nodeid
        }
    }).success((sensors) => {
        const nodeData = sensors.map((node) => node.get({ plain: true }));
    });
    

提交回复
热议问题