首页 > 解决方案 > 为什么我在使用 Sequelize 填充字段时遇到错误?

问题描述

我正在 Apollo-Server 后端使用 Sequelize。到目前为止,这很简单,但我现在在重构我的代码时遇到了一些问题。具体来说,我在填充某些字段时遇到了麻烦。

查看我包含的代码片段,comment查询的第一个和第二个版本都按预期工作。但是,第三个和第四个结果分别是Error: missing FROM-clause entry for table \"votes->votedBy\"Error: Include unexpected. Element has to be either a Model, an Association or an object.错误。

我必须User多次填充该字段(addedBy, commentedBy, votedBy)。投票也是如此,因此我决定将它们存储在各自的变量中,而不是重复输入它们的所有逻辑,如下所示。我注意到的是,如果我只使用存储字段变量 ( commentVersionTwo) 的一个实例,它就可以工作。commentVersionThree字段变量(和)的任何额外使用commentVersionFour都会中断。我在这里想念什么?

用户和投票字段

import models from '../models';

const { Profile, User, Vote } = models;

export const userFields = {
  model: User,
  include: {
    model: Profile,
    as: 'profile',
  },
};

export const voteFields = {
  model: Vote,
  as: 'votes',
  include: {
    as: 'votedBy',
    ...userFields,
  },
};

评论查询

import { userFields, voteFields } from '../populate-fields';

export default {
  Query: {
    commentVersionOne: (
      parent,
      { commentId },
      { models: { Comment, Vote, User, Profile } }
    ) => {
      return Comment.findByPk(commentId, {
        include: [
          { 
            as: 'commentedBy',
            model: User,
            include: {
              model: Profile,
              as: 'profile',
            }
          },
          {
            model: Vote,
            as: 'votes',
            include: {
              as: 'votedBy',
              model: User,
              include: {
                model: Profile,
                as: 'profile'
              }
            }
          },
        ],
      })
        .then((foundComment) => foundComment.get({ plain: true }))
        .catch((error) => {
          throw new Error(error.message);
        });
    },

    commentVersionTwo: (
      parent,
      { commentId },
      { models: { Comment, Vote, User, Profile } }
    ) => {
      return Comment.findByPk(commentId, {
        include: [
          { 
            as: 'commentedBy',
            ...userFields
          },
          {
            model: Vote,
            as: 'votes',
            include: {
              as: 'votedBy',
              model: User,
              include: {
                model: Profile,
                as: 'profile'
              }
            }
          },
        ],
      })
        .then((foundComment) => foundComment.get({ plain: true }))
        .catch((error) => {
          throw new Error(error.message);
        });
    },

    // Error: missing FROM-clause entry for table \"votes->votedBy\"
    commentVersionThree: (
      parent,
      { commentId },
      { models: { Comment, Vote, User, Profile } }
    ) => {
      return Comment.findByPk(commentId, {
        include: [
          { 
            as: 'commentedBy',
            ...userFields
          },
          {
            model: Vote,
            as: 'votes',
            include: {
              as: 'votedBy',
              ...userFields
            }
          },
        ],
      })
        .then((foundComment) => foundComment.get({ plain: true }))
        .catch((error) => {
          throw new Error(error.message);
        });
    },

    // Error: Include unexpected. Element has to be either a Model, an Association or an object.
    commentVersionFour: (
      parent,
      { commentId },
      { models: { Comment } }
    ) => {
      return Comment.findByPk(commentId, {
        include: [
          { 
            as: 'commentedBy',
            ...userFields
          },
          {...voteFields}
        ],
      })
        .then((foundComment) => foundComment.get({ plain: true }))
        .catch((error) => {
          throw new Error(error.message);
        });
    },
  }
}

标签: javascriptnode.jssequelize.js

解决方案


设法让它工作。因此,我没有使用存储各个字段的普通对象,而是使用返回所述(字段)对象的函数。


推荐阅读