所以我正在开发一个应用程序使用风帆js。我想发送一个线程连同它的评论作为响应。
这是我的模型
Thread.js
module.exports = {
attributes: {
id: {
type: "integer",
primaryKey: true,
autoIncrement: true
},
title: "string",
content: "string",
userId: {
model: "user"
},
createdAt: "string",
isSecret: "boolean",
comments: {
collection: "comment",
via: "threadId"
}
}
};
Comment.js
module.exports = {
attributes: {
id: {
type: "integer",
primaryKey: true,
autoIncrement: true
},
threadId: {
model: "thread"
},
content: "string",
createdAt: "string",
isSecret: "boolean",
userId: {
model: "user"
}
}
};
在注释中有两个嵌套的模型,User和Thread。但是响应只显示id
{
"comments": [
{
"threadId": 4,
"content": "Comment of thread one.",
"createdAt": "10-27-2016 09:39:50",
"isSecret": false,
"userId": 5,
"updatedAt": "2016-10-27T01:50:19.968Z",
"id": 3
}
],
"userId": {
"firstName": "Tio",
"LastName": "Ammar",
"email": "adityaamirullah@gmail.com",
"userName": "tioammar",
"avatar": "https://tioammar.com/avatar.jpg",
"createdAt": "2016-10-27T01:33:02.076Z",
"updatedAt": "2016-10-27T01:33:02.076Z",
"id": 5
},
"title": "Initial Thread",
"content": "Content of initial thread.",
"createdAt": "10-27-2016 09:34:50",
"isSecret": false,
"updatedAt": "2016-10-27T01:35:29.559Z",
"id": 4
}
我想展示实际的User模型。请帮忙,提前感谢!:)
我认为你正在寻找的是深/嵌套人口。这是一个类似的问题。
遗憾的是,Waterline不支持深度人口,如果你想继续使用Waterline,那么你必须做几个查询来实现这一点。其中一个额外的查询可能看起来像……
Comment.find({threadId: thread.id})
.populate("userId")
.exec(function(err, comments){
if(err)...
thread.comments = comments;
});
如果你决定这样做,你可能已经使用toJSON()的问题