Javascript 无法读取属性';芬顿';未定义的错误-Vue.js、Apollo服务器、GraphQL错误
我花了24个小时努力想弄清楚我到底做错了什么,所以我终于想我应该崩溃了,问问你们所有优秀的人,如果你们看到了什么不对劲的地方 我的Apollo服务器运行得很好,我的getUser GraphQL查询工作得很好,但我无法让getFosseReport查询工作,出现上述错误(提供的屏幕截图) 首先,我的typedefs.graphql文件显示了我的FosseReport类型以及getFosseReport查询:Javascript 无法读取属性';芬顿';未定义的错误-Vue.js、Apollo服务器、GraphQL错误,javascript,graphql,apollo,Javascript,Graphql,Apollo,我花了24个小时努力想弄清楚我到底做错了什么,所以我终于想我应该崩溃了,问问你们所有优秀的人,如果你们看到了什么不对劲的地方 我的Apollo服务器运行得很好,我的getUser GraphQL查询工作得很好,但我无法让getFosseReport查询工作,出现上述错误(提供的屏幕截图) 首先,我的typedefs.graphql文件显示了我的FosseReport类型以及getFosseReport查询: type Query { getUser(email: String!): User g
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!
}
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;
}
},
}
使用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!
}
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;
}
},
}
游乐场查询将显示错误消息:
最后是getFosseReport查询的文档,其中显示了FosseReport的返回类型等所有内容:
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);
我肯定我在这里遗漏了一些愚蠢的东西,但如果你们有任何建议,我将不胜感激
编辑:忘了为FosseReport显示我的猫鼬模型:
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);
好吧,我觉得自己很愚蠢。显然,我完全忘记了我的server.js文件,最明显的是您传递给ApolloServer的上下文对象:
const server = new ApolloServer({
typeDefs,
resolvers,
context: async ({req}) => {
const token = req.headers["authorization"];
return { User, FosseReport, currentUser: await getUser(token) };
}
});
我没有将“FosseReport”对象与上下文一起传递,只有User对象,这就是它无法工作的原因。因此,如果将来有人犯了同样的错误……现在你知道了。嗨,请不要在图像中发布代码或样本数据,图像无法搜索,因此对未来的读者没有用处,也无法复制粘贴到编辑器中并编译以再现问题。哦,对不起!我将尝试将所有内容更改为代码,但对于某些代码,它工作得不是很好,并且不确定如何将GraphQL游乐场的内容发布为代码…让我尝试编辑一些内容,使其更适合此论坛。