<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 返回的对象

      Add Property to Object that is returned by Sequelize FindOne(将属性添加到 Sequelize FindOne 返回的对象)
      • <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 返回的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Pause youtube video, youtube api(暂停 youtube 视频,youtube api)
                Youtube iframe api not triggering onYouTubeIframeAPIReady(Youtube iframe api 未触发 onYouTubeIframeAPIReady)
                How can I stop a video with Javascript in Youtube?(如何在 Youtube 中停止使用 Javascript 的视频?)
                How to call Greasemonkey#39;s GM_ functions from code that must run in the target page scope?(如何从必须在目标页面范围内运行的代码中调用 Greasemonkey 的 GM_ 函数?)
                How do you mute an embedded Youtube player?(如何使嵌入式 Youtube 播放器静音?)
                How to get number of video views with YouTube API?(如何使用 YouTube API 获取视频观看次数?)

                      <bdo id='dYASF'></bdo><ul id='dYASF'></ul>
                    • <legend id='dYASF'><style id='dYASF'><dir id='dYASF'><q id='dYASF'></q></dir></style></legend>
                          <tbody id='dYASF'></tbody>
                      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'>