Node.js orm2 update时关联字段无法修改解决方法

栏目: Node.js · 发布时间: 7年前

内容简介:Node.js orm2 update时关联字段无法修改解决方法

使用Orm2进行model修改时,发现当对关联字段进行update操作时,执行了2次SQL,第一次把值改为了我们期望的值,第二次又会把值改回成原来的值。

经过调查,当autoFetch开启时,会自动取得关联model,此时的model为:

{

roomId: xxx, // 关联的roomId

room: {

// 关联的room对象

}

}

第一次会按照roomId去修改,第二次则会按照fetch的关联对象room进行修改,导致了我们看到的执行了两次 SQL 的情况。

解决方法,在save操作前将fetch的对象删除即可。


for (var j in originEntity) {

if (_.isObject(originEntity[j])) {

delete originEntity[j];

}

}

originEntity.save(function(err) {

if (err) {

return res.json({ errMsg: err[0].msg });

}

return res.json({ code: 200, data: originEntity });

});


以上所述就是小编给大家介绍的《Node.js orm2 update时关联字段无法修改解决方法》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Web Design in a Nutshell

Web Design in a Nutshell

Jennifer Niederst / O'Reilly Media, Inc. / 2006-02-21 / USD 34.99

Are you still designing web sites like it's 1999? If so, you're in for a surprise. Since the last edition of this book appeared five years ago, there has been a major climate change with regard to web......一起来看看 《Web Design in a Nutshell》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具