sau0409
sau0409

Reputation: 153

Not getting response from mongoose model.find() query

I am trying to fetch all records from my mongo db using model and schema. I am able to connect to db. And when I run query I dont get any response.

routes.js

import express from 'express';
var router = express.Router();
import mongoose from 'mongoose';
const db = 'mongodb://localhost:27017/dbname';
import {contacts} from '../models/model.js';

// exports this js file
export { router }

//connect to mongodb

mongoose.createConnection(db, { useNewUrlParser: true , useUnifiedTopology: true }, (err, res) => {
       if(err) {
           console.error("Error: "+ err);
       }
       else {
           console.log('connected to mongodb');
       }
})


router.get('/check', (req, res)=> {
    res.send('in roues');
});

//mongo db calls

router.get('/contacts', (req, res) => {
    console.log('getting contacts');
   let promise = contacts.find({}).exec();
   console.log(promise);
   promise.then(function(err, contacts) {
       console.log('in func');
       if(contacts) {
           console.log('data received');
           console.log(contacts);
           res.send('data received');
       }
       else {
           console.log('doc not found');
       }
   })
})

model.js

    import mongoose from 'mongoose';
    const schema = mongoose.Schema;

    const contactSchema = new schema({
            name: String,
            details: [
                {
                    text: String,
                    icon: String
                }
            ]
        });

    let contacts = mongoose.model('contacts', contactSchema);

export {contacts}

Console output: (node:27440) ExperimentalWarning: The ESM module loader is experimental. server started connected to mongodb getting contacts Promise { }

Upvotes: 1

Views: 1213

Answers (3)

sau0409
sau0409

Reputation: 153

.then(...) only takes one parameter, which is the resolved response.

So instead of promise.then(function(err, contacts) { ... }); use promise.then(function(contacts) { ... });

Upvotes: 0

Irfan Sabbir
Irfan Sabbir

Reputation: 117

This can be cause of not catching the error. use then catch or try catch with async await.

  router.get('/contacts', (req, res) => {
  console.log('getting contacts');
  contacts.find({})
    .then(contacts=> {
      console.log(contacts)
    })
    .catch(error=>{
     console.log(error.mmessage)
    }

i have used es6 arrow function. You can also follow try catch with async await

  router.get('/contacts', async(req, res) => {
    try{
     console.log('getting contacts');
     const contacts = await contacts.find({})
       console.log(contacts)
    }
    .catch(error){
     console.log(error.mmessage)
    }

Upvotes: 2

Higor Tavares
Higor Tavares

Reputation: 67

you must handle the error case with the catch function, see this link

https://medium.com/@lucymarmitchell/using-then-catch-finally-to-handle-errors-in-javascript-promises-6de92bce3afc

Upvotes: 0

Related Questions