My goal is to get data from MongoDB atlas in the react app. Even after writing .then and .catch with axios.get() I'm getting an unresolved promise.
My code:
const entry = axios.get('http://localhost:3001/user')
.then(res=>{
console.log(res.data)
return res.data;
})
.catch(err=>{console.log("Error: "+err)})
console.log(entry);
The above snippet has the following output
Promise {<pending>}
(5) [{…}, {…}, {…}, {…}, {…}]
Here console.log(res.data)
is giving me the array whereas console.log(entry)
is giving me a pending promise.
Here is the nodejs code when I retrieve data from MongoDB atlas
const express = require('express');
const Route = express.Router();
const Note = require('../models/note');
Route.get('/', async(req, res)=>{
await Note.find()
.then(notes=>res.send(notes))
.catch(err=>res.send("Error: "+err))
})
module.exports = Route;