Search code examples
javascriptgraphqlapollo

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.

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).

First, my typedefs.graphql file which shows my FosseReport type, as well as the getFosseReport query:

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:

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:

playground query

And finally, the Docs for the getFosseReport query, which shows everything such as the return type of FosseReport:

Docs for playground

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:

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);

Solution

  • Okay, I feel really stupid. Apparently I completely forgot about my server.js file, most notably the context object you pass through to ApolloServer:

    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. So if anyone in the future has this same slip up...well now you know.