• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

252
Views
GraphQL Mongoose with MongoDB Atlas return empty array

I'm trying to connect MongoDB Atlas and mongoose but server always returns an empty array '[ ]'. When I load a mock data everything works (Mocked data and MongoDB Atlas have the same values) I have connect to MongoDB Atlas, but can't get some data.

Controller:

const express = require('express')
const { ApolloServer } = require('apollo-server-express')
const { createServer } = require('http')
const mongoose = require('mongoose')
const typeDefs = require('./schema')
const resolvers = require('./resolvers')

const app = express()
const server = new ApolloServer({
    typeDefs,
    resolvers,
    playground: {
        endpoint: 'http://localhost:3000/graphql',
        settings: {
            'editor.theme': 'light'
        }
    }
})
server.applyMiddleware({ app })

const httpServer = createServer(app)
server.installSubscriptionHandlers(httpServer)

mongoose
    .connect(`mongodb+srv://<username>:<password>@cluster0.brdqp.mongodb.net/${process.env.mongoDatabase}?retryWrites=true&w=majority`, { useNewUrlParser: true, useUnifiedTopology: true })
    .then((res) => {
        httpServer.listen(3000, () => {
            console.log('connected!')
        })
    })
    .catch((err) => {
        console.error('Error while connecting to MongoDB', err);
    })

Schema:

const { gql } = require('apollo-server-express')

const typeDefs = gql`
    type Todo {
        _id: String,
        title: String,
        description: String
        status: String
        date: String
    }

    type Query {
        todos: [Todo]
    }

`
module.exports = typeDefs

Resolvers:

const { Todo, Restaurant, Customer, Order } = require('./models')
const pubsub = require('./pubsub')

const resolvers = {
    Query: {
        todos(parent, args, context, info) {
            return Todo.find()
                .then(todo => {
                    return todo.map(r => ({ ...r._doc }))
                })
                .catch(err => {
                    console.error(err)
                })
        },
    },
}

module.exports = resolvers

MongoDB Atlas collection: (Database name : node, collection name in DB : todo)

_id : 5c9bdb721c9d440000345d62
title : "question"
desctiption : "test test"
status: done
date: 1

But the server always returns [ ] How???? Why&?

about 3 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error