<small id='t8du7'></small><noframes id='t8du7'>

  1. <legend id='t8du7'><style id='t8du7'><dir id='t8du7'><q id='t8du7'></q></dir></style></legend>

    <i id='t8du7'><tr id='t8du7'><dt id='t8du7'><q id='t8du7'><span id='t8du7'><b id='t8du7'><form id='t8du7'><ins id='t8du7'></ins><ul id='t8du7'></ul><sub id='t8du7'></sub></form><legend id='t8du7'></legend><bdo id='t8du7'><pre id='t8du7'><center id='t8du7'></center></pre></bdo></b><th id='t8du7'></th></span></q></dt></tr></i><div id='t8du7'><tfoot id='t8du7'></tfoot><dl id='t8du7'><fieldset id='t8du7'></fieldset></dl></div>
    <tfoot id='t8du7'></tfoot>
      <bdo id='t8du7'></bdo><ul id='t8du7'></ul>
    1. 将属性添加到 Sequelize FindOne 返回的对象

      时间:2023-09-03
      • <i id='56NBW'><tr id='56NBW'><dt id='56NBW'><q id='56NBW'><span id='56NBW'><b id='56NBW'><form id='56NBW'><ins id='56NBW'></ins><ul id='56NBW'></ul><sub id='56NBW'></sub></form><legend id='56NBW'></legend><bdo id='56NBW'><pre id='56NBW'><center id='56NBW'></center></pre></bdo></b><th id='56NBW'></th></span></q></dt></tr></i><div id='56NBW'><tfoot id='56NBW'></tfoot><dl id='56NBW'><fieldset id='56NBW'></fieldset></dl></div>

          <bdo id='56NBW'></bdo><ul id='56NBW'></ul>
            <tbody id='56NBW'></tbody>
          <legend id='56NBW'><style id='56NBW'><dir id='56NBW'><q id='56NBW'></q></dir></style></legend>

        • <tfoot id='56NBW'></tfoot>
        • <small id='56NBW'></small><noframes id='56NBW'>

                本文介绍了将属性添加到 Sequelize FindOne 返回的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                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({where: {id: 1}})
                        .then(function (cat) {
                            // cat exists and looks like {id: 1}
                            cat.name = "Lincoln";
                            // console.log of cat is {id: 1, name: Lincoln}
                            res.json(cat);
                        });
                });
                

                The client only see's {id: 1} and not the newly added key.

                • What is going on here?
                • What type of Object is returned by Sequelize?
                • How can I add new properties to my Cats and send them back?

                解决方案

                The Sequelize Model class (of which your cats are instances) has a toJSON() method which res.json will presumably use to serialise your cats. The method returns the result of Model#get() (https://github.com/sequelize/sequelize/blob/95adb78a03c16ebdc1e62e80983d1d6a204eed80/lib/model.js#L3610-L3613), which only uses attributes defined on the model. If you want to be able to set the cats name, but not store names in the DB, you can use a virtual column when defining your cat model:

                sequelize.define('Cat', {
                  // [other columns here...]
                  name: Sequelize.VIRTUAL
                });
                

                Alternatively, if you don't want to add properties to the model definition:

                cat = cat.toJSON(); // actually returns a plain object, not a JSON string
                cat.name = 'Macavity';
                res.json(cat);
                

                这篇关于将属性添加到 Sequelize FindOne 返回的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:建立关联时,哪些方法/mixins sequelize 添加到模型中? 下一篇:FindAll 包含涉及复杂的多对(多对多)关系(sequelizejs)

                相关文章

                    <bdo id='dYASF'></bdo><ul id='dYASF'></ul>
                1. <legend id='dYASF'><style id='dYASF'><dir id='dYASF'><q id='dYASF'></q></dir></style></legend>
                  1. <tfoot id='dYASF'></tfoot>
                    <i id='dYASF'><tr id='dYASF'><dt id='dYASF'><q id='dYASF'><span id='dYASF'><b id='dYASF'><form id='dYASF'><ins id='dYASF'></ins><ul id='dYASF'></ul><sub id='dYASF'></sub></form><legend id='dYASF'></legend><bdo id='dYASF'><pre id='dYASF'><center id='dYASF'></center></pre></bdo></b><th id='dYASF'></th></span></q></dt></tr></i><div id='dYASF'><tfoot id='dYASF'></tfoot><dl id='dYASF'><fieldset id='dYASF'></fieldset></dl></div>
                  2. <small id='dYASF'></small><noframes id='dYASF'>