模型仅在 Sails 的嵌套模型中显示 ID

Model only show ID in nested model in Sails

所以我正在使用 sails 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
}

我想展示实际的用户模型。请帮助并提前致谢! :)

我想你要找的是deep/nested人口Here is a similar question.

遗憾的是,Waterline 不支持深度填充,如果您想继续使用 Waterline,则必须进行多次查询才能实现。其中一个额外的查询可能类似于...

Comment.find({threadId: thread.id})
  .populate("userId")
  .exec(function(err, comments){
    if(err)...
    thread.comments = comments;
  });

如果您决定这样做,您可能已经 issues using toJSON().