0

My target is to fetch all documents in "Ingredients" collection but my code returns only empty array.

This is my "Ingredients" collection: enter image description here

ingredient model file:

const mongoose = require('mongoose');

const Schema = mongoose.Schema;


const ingredientSchema = new Schema({
    name: String,
    number: Number
})

const Ingredient = mongoose.model('Ingredients', ingredientSchema);

module.exports = Ingredient;

ingredients route file:

const router = require('express').Router();
let Ingredients = require('../models/ingredients.model');

router.route('/').get((req, res) => {

    let getIngredients = async function (){
        let ingredients = await Ingredients.find({});
        console.log(ingredients);
        res.json(ingredients)
    }
    getIngredients()
})

module.exports = router;
4
  • Try removing the empty object and calling the function without any arguments: await Ingredients.find() Commented Nov 2, 2020 at 12:00
  • why is ur path ingredients.model? Commented Nov 2, 2020 at 12:07
  • I dont know the reason, just called it like this as I saw in youtube tutorial Commented Nov 2, 2020 at 12:09
  • What you can see? Commented Nov 2, 2020 at 12:27

3 Answers 3

1

Have it implement like this instead on some blocks of your code:

const { Router } = require('express');
const router = new Router();



router.get('/', async (req, res) => {
    const ingredients = await Ingredients.find({});
    console.log(ingredients);
    
    res.json(ingredients);
})
Sign up to request clarification or add additional context in comments.

Comments

1

OK just fixed it by creating new collection with the name "ingredient" (small letter case) and changed my scheme to this one and now it works.

1 Comment

This behaviour of Mongoose's is exceedingly annoying, thank you!
0

I've encountered the same error with next.js and after some debugging I found out that the issue with my code was that I should use await before the function that connects to the Database and then make the GET request after waiting for the connection to be done. I've attached some code below.

This the code fragment the caused the error at the begging:

export default async function handler(req, res) {
    const { method } = req;

    dbConnect() // the function that connects to the database

    if(method === "GET") {
        await Trip.find()
        .then(trips => res.json(trips))
        .catch(error => res.status(500).json(error.message))
    }
}

and here is how I fixed it and it worked very well after that:

export default async function handler(req, res) {
    const { method } = req;

    await dbConnect() // just by adding await before connecting to the database

    if(method === "GET") {
        await Trip.find()
        .then(trips => res.json(trips))
        .catch(error => res.status(500).json(error.message))
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.