javascript - 为什么我在使用 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);
});
},
}
}
解决方案
设法让它工作。因此,我没有使用存储各个字段的普通对象,而是使用返回所述(字段)对象的函数。
推荐阅读
- graph - 贪心和最佳优先搜索算法有什么区别?
- cmake - CMake:将 add_library 与许多源一起使用并在 target_link_libraries 中使用时出现链接错误
- java - 暴露客户端机密是否对 oauth 2 中的隐式授权类型构成威胁?
- cloud-foundry - 我需要 cloud Foundry api 来查看日志
- node.js - Firebase 函数承诺无法正确触发
- android-architecture-components - 如何将统一响应正文添加到 ApiResponse
- java - Java // 冗余
- .net - 发布请求中的 500(内部服务器错误)(.net core 2.0)
- mysql - 如何在 Rails 中编写代码以在 SQL 中运行我想要的查询
- git - git: .git/refs/remotes/origin/master 不存在。为什么 git checkout refs/heads/origin/master 有效?