Sequelize:如何将 return 关系作为 JSON 属性?
Sequelize: How to return relationships as JSON attribute?
我有一个 articles
和 authors
的简单数据库。
每个 article
通过 belongsToMany
关系连接到 author(s)
,例如:
文章
id: 1
title: Test
作者
id: 1
name: Foo
id: 2
name: Bar
比如说,article
加入了两位 作者。
使用sequelize,我怎样才能Articles.findAll()
return 作者及其详细信息,例如:
"articles": [
{
"id": 1,
"title": "Test",
"author_ids": "[1,2]"
}
]
在您的查询中使用 include option:
Articles.findAll({
include: [Author]
}).then(function(articles) {
console.log(articles[0].authors[0].author_id);
});
我有一个 articles
和 authors
的简单数据库。
每个 article
通过 belongsToMany
关系连接到 author(s)
,例如:
文章
id: 1
title: Test
作者
id: 1
name: Foo
id: 2
name: Bar
比如说,article
加入了两位 作者。
使用sequelize,我怎样才能Articles.findAll()
return 作者及其详细信息,例如:
"articles": [
{
"id": 1,
"title": "Test",
"author_ids": "[1,2]"
}
]
在您的查询中使用 include option:
Articles.findAll({
include: [Author]
}).then(function(articles) {
console.log(articles[0].authors[0].author_id);
});