简体   繁体   English

无法读取未定义的属性“findOne”- Vue.js,Apollo 服务器,GraphQL 错误

[英]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.

相关问题 错误 Vue.js“无法读取未定义的属性‘props’” - Error Vue.js "Cannot read property 'props' of undefined" vue.js中的'无法读取未定义的属性'color1''错误 - 'Cannot read property 'color1' of undefined' error in vue.js 无法读取vue.js中“未定义”的属性 - Cannot read property of 'Undefined' in vue.js Vue.js 计算属性:[Vue 警告]:渲染错误:“TypeError:无法读取未定义的属性‘userId’” - Vue.js computed property : [Vue warn]: Error in render: "TypeError: Cannot read property 'userId' of undefined" TypeError:无法读取未定义的“已创建”属性,未定义错误“Vue” Vue.js 3 - TypeError: Cannot read property 'created' of undefined, error 'Vue' is not defined Vue.js 3 vue.js:属性或方法“”未定义,渲染错误:“TypeError:无法读取未定义的属性” - vue.js : Property or method "" is not defined, Error in render: "TypeError: Cannot read property '' of undefined" Vue.js + Firestore 无法读取未定义的属性“集合” - Vue.js + Firestore Cannot read property 'collection' of undefined Vue.js TypeError:消息无法读取未定义的属性“ singlePost” - Vue.js TypeError: Message Cannot read property 'singlePost' of undefined 无法读取for循环上未定义的.style属性(Vue.js) - Cannot read .style property of undefined on for loop (Vue.js) 无法读取未定义的属性“文本”-Vue.js - Cannot read property 'text' of undefined - Vue.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM