Add Property to Object that is returned by Sequelize FindOne

前端 未结 5 1638
孤独总比滥情好
孤独总比滥情好 2020-12-29 05:09

I am trying to add a property to a sequelize instance before passing it back to the client.

router.get(\'/cats/1\', function (req, res) {
    Cat.findOne({w         


        
5条回答
  •  时光说笑
    2020-12-29 05:21

    What works for me is to use setDataValue

    router.get('/cats/1', function (req, res) {
        Cat.findOne({where: {id: 1}})
            .then(function (cat) {
                cat.setDataValue("name", "Lincoln");
                res.json(cat);
            });
    });
    

    Specs for the function

    public setDataValue(key: string, value: any)
    
    Update the underlying data value
    
    Params:
    
    Name    Type    Attribute   Description
    key     string              key to set in instance data store   
    value   any                 new value for given key
    

    Source: https://sequelize.org/master/class/lib/model.js~Model.html#instance-method-setDataValue

提交回复
热议问题