[英]Cannot read property 'findOne' of undefined - Vue.js, Apollo Server, GraphQL error
I've been struggling trying to figure out what I'm doing wrong here for about 24 hours, so I finally figured I'd break down and ask all of you wonderful people if you see something out of place.大约 24 小时以来,我一直在努力弄清楚我做错了什么,所以我终于决定我会崩溃,如果你们看到不对劲的地方,请问你们所有很棒的人。
I've got my Apollo server running just fine, and my getUser GraphQL query works just fine, but I can't get the getFosseReport query to work, getting the above error (screenshots provided).我的 Apollo 服务器运行良好,我的 getUser GraphQL 查询工作正常,但我无法使 getFosseReport 查询工作,出现上述错误(提供的屏幕截图)。
First, my typedefs.graphql file which shows my FosseReport type, as well as the getFosseReport query:首先,我的 typedefs.graphql 文件显示了我的 FosseReport 类型,以及 getFosseReport 查询:
type Query {
getUser(email: String!): User
getCurrentUser: User
getFosseReport(facility_id: String!, report_date: String!): FosseReport
}
type Token {
token: String!
}
type FosseReport {
_id: ID
facility_id: String!
report_date: String!
reports: [FosseItems]
created_date: String
modified_date: String
}
type FosseItems {
account_desc: String
fytd_cgs: String
fytd_credit: String
fytd_debit: String
ptd_cgs: String
ptd_credit: String
ptd_debit: String
seq_cg_cd: String
today_cgs: String
today_credit: String
today_debit: String
}
type User {
_id: ID
email: String!
password: String!
facility_id: String!
}
My resolvers file with the getFosseReport query:我的带有 getFosseReport 查询的解析器文件:
const bcrypt = require('bcrypt');
const jwt = require('jsonwebtoken');
const createToken = (user, secret, expiresIn) => {
const { username, email } = user;
return jwt.sign({username, email}, secret, {expiresIn});
};
module.exports = {
Query: {
getUser: async (_, {email}, {User}) => {
const user = await User.findOne({email});
if(user) {
return user;
}
return "None Found";
},
getFosseReport: async(_, {facility_id, report_date}, {FosseReport}) => {
const fosseReport = await FosseReport.findOne();
if(!fosseReport) {
return null;
}
return fosseReport;
},
getCurrentUser: async (_, args, { User, currentUser }) => {
if(!currentUser) {
return null;
}
const user = await User.findOne({email: currentUser.email});
return user;
}
},
}
The playground query with the error message:带有错误消息的游乐场查询:
And finally, the Docs for the getFosseReport query, which shows everything such as the return type of FosseReport:最后是 getFosseReport 查询的文档,它显示了所有内容,例如 FosseReport 的返回类型:
I'm sure there's something stupid I'm missing here, but if any of you have any suggestions I would greatly appreciate it.我确定我在这里遗漏了一些愚蠢的东西,但如果你们有任何建议,我将不胜感激。
Edit: Forgot to show my mongoose model, too, for the FosseReport:编辑:对于 FosseReport,也忘记显示我的 mongoose model:
const mongoose = require('mongoose');
const FosseReportSchema = new mongoose.Schema({
facility_id: {
type: String
},
report_date: {
type: Date
},
reports: {
type: mongoose.Schema.Types.ObjectId,
required: true,
ref: "FosseReportPart"
},
created_date: {
type: Date,
default: Date.now
},
modified_date: {
type: Date,
default: Date.now
}
});
module.exports = mongoose.model('FosseReport', FosseReportSchema);
Okay, I feel really stupid.好吧,我真的觉得自己很蠢。 Apparently I completely forgot about my server.js file, most notably the context object you pass through to ApolloServer:显然我完全忘记了我的 server.js 文件,最值得注意的是你传递给 ApolloServer 的上下文 object:
const server = new ApolloServer({
typeDefs,
resolvers,
context: async ({req}) => {
const token = req.headers["authorization"];
return { User, FosseReport, currentUser: await getUser(token) };
}
});
I did not have the "FosseReport" object being passed with the context, only the User object, which is why it wasn't working.我没有通过上下文传递“FosseReport”object,只有用户 object,这就是它不起作用的原因。 So if anyone in the future has this same slip up...well now you know.因此,如果将来有人犯了同样的错误……那么现在您知道了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.