Mongoose find not returning the full collection - node.js

I am trying to read data from an html form through a POST, store it in a mongoDB and query it using model.find() and print it in console. But when i run this for the first time the find() is returning an empty object and on giving the next input the previous data excluding the current input is retrieved by th find(). How can i print the full collection including the freshly entered data
app.post("/", function(req, res){
postTitle = req.body.postTitle;
postDesc = req.body.postDesc;
const post = new Post({
title:postTitle,
desc:postDesc
});
post.save();
Post.find({}, function(err, data){
if(err){
console.log(err);
}else{
console.log(data);
}
});
//console.log(postTitle, postDesc);
});

The command post.save(); will just begin working and your code will continue meanwhile. When your Post.find({} ... starts working, your post.save(); haven't finished working, and thus you're not getting the results.
Change the function so you wait for the save to give you a callback with an ok and then you can query the database.
app.post("/", function(req, res) {
const postTitle = req.body.postTitle;
const postDesc = req.body.postDesc;
const post = new Post({
title: postTitle,
desc: postDesc
});
post.save(function(err) {
if (err) {
// Something went wrong with the save, log and return the error message
console.error(err);
return res.send(err);
}
console.log(`Post "${postTitle}" saved to database.`);
// Since we know that the post has been saved, continue querying the database.
Post.find({}, function(err, data) {
if (err) {
// Something went wrong with the query, log and return the error message
console.error(err);
return res.send(err);
}
console.log(data);
res.send(data);
});
});
});
This code is not tested.
You can also try async/await out, see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function and also mongoose documentation for promises & async/await https://mongoosejs.com/docs/promises.html.
I myself would write the function like this using async/await and es6.
app.post('/', async(req, res) => {
const post = new Post({
title: req.body.postTitle,
desc: req.body.postDesc
});
try {
await post.save();
const posts = await Post.find();
console.log(posts);
} catch (err) {
console.error(err);
}
res.end();
});

You can try with exec
Post.find({}).exec(function (err, d) {
if(err){
console.log(err);
}else{
console.log(d);
}
});
Or try to use async await to make sure your query is running step by step
const user_info = await Post.find({});
This code is not tested

here post.save() is an async function that means it does not complete immediately. You need to use async - await in order to wait for the save() function to finish and then you query the database.

Related

MongoDB Atlas/Cloud retrieving data from _id

I am creating a project with MongoDB Atlas/Cloud and I have the method to obtain ALL the data of a database that I have created from the cloud.
This is how I did it:
router.get('/', (req, res) => {
// http://localhost:3000/data
client.connect(async err => {
if (await err) {
console.log("Error connecting to MongoDB Cloud:\n\t" + err.toString());
}
console.log("Connected to the DB!");
const collectionData = client.db("myDatabase").collection("data");
try {
const data = await collectionData.find().toArray();
console.log(data);
res.json(data);
} catch (e) {
res.json({message: e});
}
await client.close();
});
});
But I would like to be able to collect each data individually by introducing its _id in the URI. This is how I tried to do it:
router.get('/:dataID', (req, res) => {
// http://localhost:3000/data/<ID>
client.connect(async err => {
if (await err) {
console.log("Error connecting to MongoDB Cloud:\n\t" + err.toString());
}
console.log("Connected to the DB!");
const collectionData = client.db("myDatabase").collection("data");
try {
console.log(req.params.dataID); // It works, it prints the _id
const specificData = await collectionData.findById(req.params.dataID).toArray();
console.log(specificData);
res.json(specificData);
} catch (e) {
res.json({message: e});
}
await client.close();
});
});
But it doesn't work. I'm retrieving a blank object: message: {}. I've checked the _id introduced, it's exactly the same as the _id from the mongoDB object that I'm trying to retrieve. What am I doing wrong? It should be the const specificData = await collectionData.findById(req.params.dataID).toArray(); but I don't know what to change to make it work right.
P.S.: After making a GET petition to the server to /data, I get the data, but if I try to make another petition after that, I get an MongoDB Error, do anyone know why is this happening? Thank you in advance
Solved:
const specificData = await collectionData.find(req.params.dataID).toArray();
console.log(specificData.get[0]);

findById not working in mognodb with nodejs

This is my code;
I'm try for update user data. like so you are seen this code. But result coming null.
I'm not understand. please help me guys
// #Router UPDATE /updateforslip/:id
// #desc UPDATE update by ID
// #access Private
router.put('/update/user/data/:id', async (req, res) => {
var update = Pan.findByIdAndUpdate(req.params.id, {
$set: req.body
})
console.log(update)
update.exec((error, data) => {
if (error) throw error;
res.json(data)
})
})
Coming result
in console.log(update)
null
I'm using postman for request.
try this:
router.put('/update/user/data/:id', async (req, res) => {
tyr{
var update = await Pan.findByIdAndUpdate(req.params.id, {$set: req.body})
res.json(update)
}catch (err){
throw error;
}
Just try like this, don’t use exec() when you want to uae async await
and use try/carch for handling errors
try{
var data = await Pan.findByIdAndUpdate(req.params.id, {
$set: req.body
}).lean();
res.json(data)
},
catch(error){
}

GET request for specific id returning null

I'm attempting to build a simple CRUD application using express and mongodb. My GET request for all database entries is working and my POST request is working but I can't seem to figure out the problem with my GET request for individual entries.
Server.js GET request:
app.get('/api/:id', function (req, res) {
var id = req.params.id;
db.collection('api').findOne({_id: id}, (err, result) => {
if (err){
res.sendStatus(404);
return console.log(err);
}
res.redirect('/');
return console.log(result);
});
});
When I type 'url/api/593555074696601afa192d7f' which is an ID I know exists the console.log is returning null, I'm not sure if I'm doing something wrong?
You should pass ObjectID instance in the query.
let ObjectID = require("mongodb").ObjectID;
app.get('/api/:id', function (req, res) {
var id = req.params.id;
db.collection('api').findOne({_id: ObjectID(id)}, (err, result) => {
if (err){
res.sendStatus(404);
return console.log(err);
}
res.redirect('/');
return console.log(result);
});
});
Give Mongoose a try. It might be of help if your models get complex.

find all documents using mongoose

Im trying to find all documents in a DB using mongoose but I cant do it
I want to do it this way but I don't know what's wrong with it
app.get('/users', function (req, res){
User.find({}, 'name', function(err, user){
if(err){
console.log(err);
}else{
res.render('user-list', {
name : user.name
});
console.log('retrieved list of names' + user.name);
}
})
})
When I use User.findOne({}, 'name', function(err, user){.. I get back the first doc which is what I would expect. Please explain why the code above is not allowing me to get all documents. I feel like I'm using it the right way as show in the mongoose doc
Edit
thanks for help guys
i did like this:
app.get('/users', function (req, res){
User.find({}, 'name', function(err, users){
if(err){
console.log(err);
}else{
res.render('user-list', {
name : users.map(function(doc){
return doc.name + "<br>"
})
});
console.log('retrieved list of names' + users.name);
}
})
})
can some one please help me with getting each name on a new line the "<br>" shows up on the page but it doesn't make a new line "<br>,Joe<br>,mike<br>"
Jade:
extend layout
block content
p list of users #{name}
app.get('/users', function (req, res){
User.find({}, 'name', function(err, users){
if(err){
console.log(err);
} else{
res.render('user-list', users);
console.log('retrieved list of names', users.length, users[0].name);
}
})
});
As said in the comments, find can find many objects, so it returns an array as contrasted with findOne that returns a single object. Adjust for that as above and you should be back on track.
app.get(`/users`, async (req,res)=>{
try{
const users = await User.find({}).exec()
users && res.render('user-list',users)
}catch(error){
res.status(500).json({error})
}
})
//OR
app.get(`/users`, async (req,res)=>{
try{
const users = await User.find({}).exec()
const list = new Array()
for(let row of users){
list.push(`${users.name}<br>`)
}
list ? res.status(200).send(list) : res.status(200).json({message: 'Users list is empty'})
}catch(error){
res.status(500).json({error})
}
})
In second example you send to client a text line!!!

After form submission, new data does not appear without page reload

In my ExpressJS app I have two routes to the same location, one for 'get' and one for 'post'.
On the 'get' page it dumps all the documents from my MongoDB collection, via MongooseJS, followed by a form to add a new record to the collection.
On the 'post' page it takes in the form data and adds it to the collection, and then displays the same page you see via 'get'.
It works, but after the form is submitted the new record doesn't appear unless I reload the page. I put the code that renders the page at the very bottom, below the part that adds the data to the collection so in my mind that should work but it doesn't.
exports.index = function(req, res){
Server.find({},
function(err, docs) {
if (!err){
res.render('servers', { title: 'verify', results: docs});
}
else { console.log(err);}
}
);
}
exports.add = function(req, res){
newServer = new Server({
name: req.body.name,
os: req.body.os,
osVersion: req.body.osVersion
});
newServer.save(function (err) {
if (err) {
console.log(err);
}
});
Server.find({},
function(err, docs) {
if (!err){
res.render('servers', { title: 'verify', results: docs});
}
else { console.log(err);}
}
);
}
Ok, I seem to make this mistake over an over again with callbacks. I fixed the problem.
exports.add = function(req, res){
newServer = new Server({
name: req.body.name,
os: req.body.os,
osVersion: req.body.osVersion
});
newServer.save(function (err) {
if (err) {
console.log(err);
} else {
Server.find({},
function(err, docs) {
if (!err){
res.render('servers', { title: 'verify', results: docs});
}
else { console.log(err);}
}
);
}
});
}
Yes, those DB queries are async, so callbacks will solve this issue. However, you should look into using promises. Mongoose returns promises by default or you could import your library of choice. They will come in handy when dealing with nested callbacks and queries and also Error handling.

Resources