Node.js续集嵌入有多个ID [英] Node.js sequelize embed hasMany IDs
本文介绍了Node.js续集嵌入有多个ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用Ember数据的Ember应用程序,并使用Sequelize.js从MySQL使用node.js后端服务数据。
I am experimenting with a Ember app using ember-data and a node.js backend serving data from MySQL using Sequelize.js.
我的问题:
如果我有一个与Post模型相关联的注释模型,那么通过ember-data的预期JSON看起来像
My problem: If I have a Comment model associated to a Post model through hasMany the expected JSON by ember-data looks like
{
"post": {
"comment_ids": [1, 2, 3]
}
}
使用续集查询/生成此JSON而不需要昂贵的循环等的最佳方式是什么?
What would be the best way to query / generate this JSON without expensive loops etc. using sequelize?
评论模型有一个带有post_id的外键。
The Comment model has a foreign key with the post_id.
推荐答案
我去执行它,如:
// include comments
Post.all({
where: "..",
include: [Comment]
}).success(function(posts) {
// collect IDs
_.each(posts, function(element) {
element["comments"] = _.pluck(element.comments, 'id');
});
});
这篇关于Node.js续集嵌入有多个ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文