相当简单的问题,只是找不到好的/干净的方法来做到这一点而不调用另一个 find
我已经用 Angular-Resource 安装了我的 Node 应用程序,我只是在对新的或更改的数据进行数据调用之类的往返。
所以ngResource制作$save()
调用我的/api/users/:id
等等。 Node 通过创建或查找用户、进行更新并保存它们来响应此调用。
是否通过create()
或 save()
,它返回创建的记录,现在,我使用 res.json(user)
溢出为我的 Angular 创建/返回的记录,以处理用更新的信息填充我的 View
现在,我知道 Sequelizes find()
和 findAll()
方法,我可以使用findAll({ include: [{ all: true }]})
或单独指定我的模型。
我想知道的是,在保存/创建时获取我的记录关联的最佳方法是什么
不幸的是,这不起作用:
models.User.create(newuser, {include:[{ all: true }]}).then(function(user) {
res.json(user);
});
我真的需要再做一次吗
find()
只是为了让我的托管模型协会?
最佳答案
为了更好地说明 RedactedProfile 中选择的解决方案的评论,这里是代码。
models.User
.create(newuser, {include:[{ all: true }]})
.then(user => {
user.reload().then(user => { res.json(user); })
});
关于javascript - 使用 Sequelize 在创建或保存时获取关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28995597/