.push() not adding MongoDB objects to array - node.js

I have set up node and mongodb and have imported some yelp data into mongo. When I query using the mongo shell, I can see there are documents and everything is fine. However I'm unable to pass them along by adding them to an array and returning that array. When I hit up localhost:3000/api/reviews, I get a blank page. My console does log everything though so the node driver for mongo is working in getting the documents. Any ideas? I feel like it has something to do with the asynchronous nature of node.
var express = require('express');
var router = express.Router();
var MongoClient = require('mongodb').MongoClient;
var assert = require('assert');
var url = 'mongodb://localhost:27017/YelpDB';
var getReviews = function(db, callback) {
var cursor = db.collection('reviews').find( );
//JSONArray jsonarray = new JSONArray();
var data = [];
cursor.each(function(err, doc) {
assert.equal(err, null);
if (doc != null) {
var jsonDoc = JSON.stringify(doc);
console.log(typeof jsonDoc);
data.push(jsonDoc);
} else {
callback();
}
});
return data;
};
router.get('/reviews/', function(req, res, next) {
//res.send('respond with a resource');
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
var data = getReviews(db, function() {
db.close();
});
res.json({"reviews": data});
});
});

Please try this one, you should return the data at the end of cursor.each in the callback function.
var getReviews = function(db, callback) {
var cursor = db.collection('reviews').find( );
var data = [];
cursor.each(function(err, doc) {
if (err)
callback(err);
if (doc) {
var jsonDoc = JSON.stringify(doc);
console.log(typeof jsonDoc);
data.push(jsonDoc);
} else {
// at the end of cursor, return the data through callback
callback(null, data);
}
});
};
router.get('/reviews/', function(req, res, next) {
//res.send('respond with a resource');
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
getReviews(db, function(err, data) {
if (err)
throw err;
// send the data in callback function
res.json({"reviews": data});
db.close();
});
});
});

Related

nodejs mongodb driver native query issue

It turns a funny error in Node.js mongodb, if I put the form below:
locals.collection.find(filter, req.query);
returns me the following error:
MongoError: query selector must be an object
Now if I manually put the query works.
I'm having this problem a long time, and I've tried several ways.
locals.collection.find({slug:'somak'}, req.query);
I am using the following function:
exports.findAll = function(req, res, next) {
var locals = {},
section = req.params.section,
query = req.query,
filter = {};
if(query.filter) {
filter = query.filter.replace(/"(\w+)"\s*:/g, '$1:');
filter = filter.replace(/["]/g, "'");
}
console.log(filter);
delete query.filter;
async.series([
function(callback) {
MongoClient.connect(url, function(err, db) {
if (err) return callback(err);
locals.collection = db.collection(section);
callback();
});
},
function(callback) {
locals.collection.count(filter, function (err, result){
if (err) return callback(err);
locals.count = result;
callback();
});
},
function(callback) {
var cursor = locals.collection.find({slug:'somak'}, req.query);
if(req.query.page) {
cursor = cursor.skip(Math.abs(req.query.limit) * --req.query.page);
}
cursor.toArray(function(err, docs) {
if (err) return callback(err);
locals.docs = docs;
callback();
});
}
],
function(err) { //This function gets called after the three tasks have called their "task callbacks"
if (err) return next(err);
// Here locals will be populated with 'count' and 'docs'
res.json({
count: locals.count,
data: locals.docs
});
});
Just use JSON.parse:
exports.findAll = function(req, res, next) {
var locals = {},
section = req.params.section,
query = req.query,
filter = {};
if(query.filter) {
filter = JSON.parse(query.filter);
}
delete query.filter;
...

mongodb native driver error on query

I am writing the filter using mongodb native driver, but it's driving me this error when you run the query.
In the case of this driver, it has no exec?
What is another way to perform this query?
exports.findAll = function(req, res) {
MongoClient.connect(url, function(err, db) {
var section = req.params.section;
var collection = db.collection(section);
var filter = req.query.filter ? {nameToLower: new RegExp('^' + req.query.filter.toLowerCase())} : {};
var query = collection.find(filter);
var count = 0;
collection.count(filter, function (error, result) {
count = result;
});
if(req.query.order) {
query.sort(req.query.order);
}
if(req.query.limit) {
query.limit(req.query.limit);
if(req.query.page) {
query.skip(req.query.limit * --req.query.page);
}
}
query.exec(function (error, results) {
res.json({
count: count,
data: results
});
});
});
};
Error:
TypeError: undefined is not a function
Better to use the async library in this case as it simplifies the code. In the case where you need to run multiple tasks that depend on each other and when they all finish do something else, use the
async.series() module. The following demonstrates how you can go about this in your case:
exports.findAll = function(req, res) {
var locals = {},
section = req.params.section,
filter = !!req.query.filter ? {nameToLower: new RegExp('^' + req.query.filter.toLowerCase())} : {};
async.series([
// Connect to DB
function(callback) {
MongoClient.connect(url, function(err, db) {
if (err) return callback(err);
locals.collection = db.collection(section); //Set the collection here, so the next task can access it
callback();
});
},
// Get count
function(callback) {
locals.collection.count(filter, function (err, result){
if (err) return callback(err);
locals.count = result; //Set the count here
callback();
});
},
// Query collection
function(callback) {
var cursor = locals.collection.find(filter);
if(req.query.order) {
cursor = cursor.sort(req.query.order);
}
if(req.query.limit) {
cursor = cursor.limit(req.query.limit);
if(req.query.page) {
cursor = cursor.skip(req.query.limit * --req.query.page);
}
}
cursor.toArray(function(err, docs) {
if (err) return callback(err);
locals.docs = docs;
callback();
});
}
], function(err) { //This function gets called after the three tasks have called their "task callbacks"
if (err) return next(err);
// Here locals will be populated with 'count' and 'docs'
res.json({
count: locals.count,
data: locals.docs
});
res.render('user-profile', locals);
});
};

Why can not read property when find data in node with mongoDB?

Below is my code
var mongodb = require('mongodb');
var MongodbClient = mongodb.MongoClient;
MongodbClient.connect('mongodb://localhost/test', function(err, db) {
db.collection('contact', function(err, collection) {
collection.find({}, function(err, rows) {
for(var index in rows)
console.log(rows[index]);
});
});
var contact = db.collection('contact');
contact.insert({
name:'Fred',
tel:'123456789',
address: 'Mars',
}, function(err, docs) {
if(err){
console.log("failed")
return;
}
else{
console.log('Success');
}
});
contact.find({}, function(err, docs) {
if(err) {
console.log("Can not find any!");
return;
}
for(var index in docs) {
console.log(docs.length);
var doc = docs[index];
console.log(doc.name);
}
});
});
I can find the data using mongodb shell but in node, it shows
TypeError: Cannot read property 'name' of null
on console.log(doc.name).
and also shows "undefined" when try to console.log(docs.length);
Did I do something wrong?
You are not using correctly the mongodb node API:
You should have:
contact.find({}).toArray(function(err,results) {
if (err) {
console.debug(err);
return;
}
console.debug(JSON.stringify(results));
});
More on docs.
You can try JSON.stringify(object) and then JSON.parse(object) and then try to access the property.
let xxxxxxxx = async (req, res) => {
let resultSet = [];
let object;
let perDayData = await yyyyyyyyyyy.find({});
perDayData = JSON.stringify(perDayData);
perDayData = JSON.parse(perDayData);
console.log(perDayData[0].field);
}

"object is not a function" error during MongoDB document insertion from a CSV stream using async.queue

I'm trying MongoDB document insertion from a CSV stream using async.queue.
But I face this following error. I've tried all the remedies given in similar SO posts.
Exact error message is:
C:\Users\admin\node_modules\mongodb\lib\mongo_client.js:406
throw err
TypeError:object is not a function
at C:\Users\admin\Desktop\mynodefile.js:13:2
at C:\Users\admin\node_modules\mongodb\lib\mongo_client.js:403:11
at process._tickCallback(node.js:355:11)
node.js code I used:
var csv = require('csv');
var async = require('async');
var fs = require('fs');
var MongoClient = require('mongodb').MongoClient;
MongoClient.connect('mongodb://localhost:27017', function(err, db) {
if (err) throw err;
var collection = db.collection('myCSVs');
var queue = async.queue(collection.insert.bind(collection), 5);
csv()
.from.path('./input.csv', { columns: true })
.transform(function (data, index, cb) {
queue.push(data, function (err, res) {
if (err) return cb(err);
cb(null, res[0]);
});
})
.on('error', function (err) {
console.log('ERROR: ' + err.message);
})
.on('end', function () {
queue.drain = function() {
collection.count(function(err, count) {
console.log('Number of documents:', count);
db.close();
});
};
});
});
You haven't mentioned the database name in your MongoClient.connect function call. You can do so like this:
MongoClient.connect('mongodb://localhost:27017/database_name',function(err, db) {
Then you can do:
var collection = db.collection('myCSVs');
If myCSVs is a collection inside database_name
Or you can also do:
MongoClient.connect('mongodb://localhost:27017',function(err, mongoclient) {
var db = mongoclient.db('database_name');
var collection = db.collection('myCSVs');
});
You have to change
var queue = async.queue(collection.insert.bind(collection), 5);
Into:
var q = async.queue(function (task, callback) {
console.log('hello ' + task.name);
callback();
}, 2);
IN this line :
queue.push(data, function (err, res) {
if (err) return cb(err);
cb(null, res[0]);
});
you are calling push with data and with a callback, but its not implemented in your
var queue = async.queue(collection.insert.bind(collection), 5);

nodejs mongodb cursor problems

I have this function that I want to return the value of a mongo query but I am getting undefined values. I am attempting to read the Email address of all of the users in the users collection of my test database.
This is my code:
var mongo = require('mongodb').MongoClient;
var connectionString = "mongodb://cannotdivulge.info.com:53778/testdb";
var users;
var db = mongo.connect(connectionString, function(err, db) {
if(err)
throw err;
console.log("connected to database");
users = db.collection('users');
});
exports.findAll = function(req, res) {
var cursor = users.find();
var result;
cursor.each(function(err, doc) {
if(err)
throw err;
result = doc.Email;
});
res.send(result);
};
result should be equal to the email address but it's undefined. The value of doc.Email is not being preserved. When I place res.send() inside cursor.each(), it will return a value.
Also, I will need to use the result variable in the function before actually returning it's value. That is why I need it's value to be preserved.
Your findall function is async. You'll need to return the results only after completing the list. In your code, the function was returning the value of result before the first callback for each had started.
exports.findAll = function(req, res) {
var cursor = users.find();
var result = [];
cursor.each(function(err, doc) {
if(err)
throw err;
if (doc === null) {
// doc is null when the last document has been processed
res.send(result);
return;
}
// do something with each doc, like push Email into a results array
result.push(doc.Email);
});
};

Resources