航行-通过一对多关系返回相关字段

时间:2019-05-21 13:03:04

标签: javascript sails.js

我想返回与用户相关的所有数据-因此这将返回来自Answer,Article,ArticleDetails和User表的数据。

我正在使用的查询是

user = await User.findOne({id:user_id}).populateAll()

它将返回除ArticleDetails表以外的所有内容。用户之间的关联为User.id = Answer.owner = Article.expert-然后,我想返回指定ArticleDetails的所有记录User

我尝试使用articles: { collection: 'articleDetails', via: 'answer', through:'article' },但没有返回与articleDetails相关的任何信息

模型是:

ArticleDetails.js

module.exports = {
  attributes: {
    question: {
      model: "question"
    },
    articleUrl: {
      type: "string"
    },
    articleMeta: {
      type: "string"
    },
    metaDescription: {
      type: "string"
    },
    metaTitle: {
      type: "string"
    },
    domain: {
      type: "string"
    },
    metaImage: {
      type: "string"
    }
  }
};

Article.js

module.exports = {
  tableName: "article",
  attributes: {
    article: {
      model: "articledetails",
    },
    answer: {
      model: "answer"
    },
    expert: {
      model: "user"
    },
  },
};

Answer.js

module.exports = {
  tableName: "answer",
  attributes: {
    owner: {
    model: "user"
    },
    text: {
      //richtext stringified contains the answer text
      type: "string" // richtext with links
    articles: {
      collection: 'articleDetails',
      via: 'answer',
      through:'article'
    }
  },

User.js

module.exports = {
  attributes: {
    user_type: {
      type: "number", // expert 1, journalist 2
      required: true
    },
    firstname: {
      type: "string",
      required: true
    },
    lastname: {
      type: "string",
      required: true
    },
    email: {
      type: "string",
      unique: true,
      required: true
    },
    questions: {
      collection: "question",
      via: "owner"
    },
    answers: {
      collection: "answer",
      via: "owner"
    },
    articles: {
      collection:'article',
      via:'expert',
   }
}};

0 个答案:

没有答案