Node JS mongoose Query is not producing result - node.js

Been stuck on this problem for hours. The below code:
router.route("/contact")
.get(function(req,res){
var response = {};
Contact.find({},function(err,data){
if(err) {
response = {"error" : "Error fetching data"};
} else {
response = {"message" : data};
}
res.json(response);
});
})
Above query produces result with all the contacts in the database but
router.route("/contact/department/:dept")
.get(function(req,res){
var response = {};
var arrDept = req.params.dept.split(",");
if(arrDept.length == 0){
response = {"error": " Please enter Department keywords"};
}
else{
response = {};
arrDept.forEach(function(currentValue){
Video.find({dept: '/'+currentValue+'/i'}, function(err, data){
if(err){
response[currentValue] = "No data found";
}else{
response[currentValue] = data;
}
});
});
}
res.json(response);
});
this code does not produce any output.
The code is entering the forEach loop in the else block but the query is not generating any result even if I modify the query to a basic one like
Video.find({},function(err, data){
if(err){
response[currentValue] = "No data found";
}else{
response[currentValue] = data;
}
});
The response JSON is still returned blank.
PS: Problem has been simplified as is only an example of actual problem i am facing in the code.
update after answer found.

res.json(response)
was written outside the query that is why a blank json was getting returned. The above scenario can be solved using promise as follows:
var promise = Contact.find({ dept: { $in: arrayDept }}).exec();
promise.then(function(resultJson) { res.json(resultJson); });
This can be used to execute all the queries in the array and return the complete json.

Your res.json(response); in the non-working example is outside the callback of your MongoDB query, that is you are writing the response before the query's callback was actually executed and therefore your result is empty.

Related

Why can't Restful pass body into database

I'm creating a RESTful API.
I wanna use GET method to check if lastName exists. If it can find lastName, return "YES", otherwise, call a POST method to create a data with lastName entered.
The problem is that it can create a new data, but the body is empty. Ideally, it should contain a value with lastName, like "lastName": "James",
{
"_id": "58a22c3c3f07b1fc455333a5",
"__v": 0
}
Here is my code.
router.route("/findLastName/:id")
.get(function(req,res){
var response;
mongoOp.findOne({deviceID: req.params.id}, function(err, result){
if (err) {
response = {"error" : true,"message" : "Error fetching data"};
res.json(response);
}
if (result) {
response = "YES";
res.send(response);
} else {
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
var xhr = new XMLHttpRequest();
var POSTurl = "http://localhost:6002/users";
var params = "lastName=" + req.params.id;
xhr.open("POST", POSTurl, true);
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.send(params);
}
});
})
PS: GET method works well, not a issue.
Let me modify a bit of your code and add comments as pointers:
// changed findLastName to find-last-name. It's a common convention,
// urls need to be case insensitive. It doesn't concern lastName, as
// that's a parameter, internal to your app so it's fine.
// even better if you name the route `find-or-create` or something, to better
// reflect what you're doing.
router.route("/find-last-name/:lastName")
.get(function(req,res){
var response;
mongoOp.findOne({deviceID: req.params.lastName}, function(err, result){
if (err) {
response = {"error" : true,"message" : "Error fetching data"};
// Adding a `return statement here. If you don't return, you'll tell
// the user that there was an error, but your code continues running
// potentially calling that res.json twice.
// Also, since it's an internal error, it's common to tell the client
// about it, by setting the status to 500
return res.status(500).json(response);
}
if (result) {
// turning the message to JSON instead. You started that above,
// and for the sake of your clients (your frontend), it's
// better to stick to JSON. Also you can pass useful info, such as
// _id of the document.
// Again adding a `return` here, and then the rest of the code
// is nested one level less. not required, but some people like that.
response = {
message: "Last name exists."
};
return res.json(response);
}
// Here begins the new code. I'm typing what I can infer from your code,
// I don't know if your MongoDB driver looks like that exactly.
mongoOp.insert({
deviceId: req.params.lastName
// add other optional properties here.
}, function (err, response) {
if (err) {
var message = {
error: true,
message: 'Cannot save new entry.'
}
return res.status(500).json(message);
}
// if we're here, everything went ok. You can probably return
// the _id of the given user.
return res.json({
message: 'New user created.',
_id: response._id
});
});
});
})

unable to add property to the json object

I am trying to add status to a response on successful update but I am not able to add the status property to json object of form. Here is my code
apiRouter.post('/forms/update', function(req, res){
if(req.body.id !== 'undefined' && req.body.id){
var condition = {'_id':req.body.id};
Form.findOneAndUpdate(condition, req.body, {upsert:true}, function(err, form){
if (err) return res.send(500, { error: err });
var objForm = form;
objForm.status = "saved successfully";
return res.send(objForm);
});
}else{
res.send("Requires form id");
}
});
and here is the response that I get, notice status is missing
{
"_id": "5580ab2045d6866f0e95da5f",
"test": "myname",
"data": "{\"name\":3321112,\"sdfsd\"344}",
"__v": 0,
"id": "5580ab2045d6866f0e95da5f"
}
I am not sure what I am missing.
Try to .toObject() the form:
Form.findOneAndUpdate(condition, req.body, {upsert:true}, function(err, form){
if (err) return res.send(500, { error: err });
var objForm = form.toObject();
objForm.status = "saved successfully";
return res.send(objForm);
});
Mongoose query result are not extensible (object are frozen or sealed), so you can't add more properties. To avoid that, you need to create a copy of the object and manipulate it:
var objectForm = Object.create(form);
objectForm.status = 'ok';
Update: My answer is old and worked fine, but i will put the same using ES6 syntax
const objectForm = Object.create({}, form, { status: 'ok' });
Another way using spread operator:
const objectForm = { ...form, status: 'ok' }
Try changing res.send(objForm) to res.send(JSON.stringify(objForm)). My suspicion is that the the Mongoose model has a custom toJson function so that when you are returning it, it is transforming the response in some way.
Hopefully the above helps.
Create empty object and add all properties to it:
const data = {};
data._id = yourObject._id; // etc
data.status = "whatever";
return res.send(data);
Just create a container.
array = {};
Model.findOneAndUpdate(condition, function(err, docs){
array = docs;
array[0].someField ="Other";
});

how get return value (of mongoose query output within model.exports ) to a calling module

For a project, I am working to get return value to main application module post validation of user and password from a mongoDB using mongoose. I am able to printout result post "User.findOne" using console.log (commented in code) but fail to pass this result to main module "app.js". I am getting "undefined" as output or initialised value of "result" if do in the User.js module.
Need help to know how to get return value (of "result" in below code) from User.js to app.js.
I am not looking for return this value using "res.send" as web response. which i found mostly while exploring google for the issue.
Thanks.
app.js
var user = require ('./User');
console.log(user.authentication("user","pass"));
User.js
var User = require('./models/Mongoose');
exports.authentication = function(user, pass){
var query = {uid: user};
var result;
//var result = "None";
User.findOne(query, function(err,obj) {
if (err) {
result = "error: find method";
} else {
if (obj != null) {
if ( pass == obj.password) {
//console.log("success: authorisation successful");
result = "authentication successful";
} else {
//console.log("error: password incorrect");
result = "authentication failed";
}
} else {
result = "error: absent user";
}
}
});
return result;
};
Output of app.js:
undefined
You return result before User.findOne has any opportunity to give you something. Instead you should add a callback function to authentication method e.g.
exports.auth = function(user, pass, callback){
User.findOne(...., function(err, user){
var result = !!user;
callback(err, result);
});
}
Anyway, you should get used to asynchronous code to feel comfortable working with node. These tutorials are likely to help you: How do I get started with Node.js

undefined value after query a second call in mongo db in node js

I am getting an undefined value for stateName when i call a second query. I know it is the problem due to asynchronus, I have searched for it in the google for getting the concept of callbacks but couldn't. below is the code.
exports.getEmployee = function(req, res) {
var jsonArray = [];
var jsonString;
var success = true;
if(success == true){
mongooseDBObject.var_employee.find(jsonString, function(err, doc_employee){
if(err == null) {
if(doc_employee_travel == "") {
res.status(404);
res.json({result: "Employee record not found."});
} else {
doc_employee.forEach(function(docEmployee){
mongooseDBObject.var_states.find({stateID: docEmployee.statename},function(err, states){
states.forEach(function(statesLoop){
stateName = statesLoop.stateName;
});
});
console.log(stateName); //showing undefined.
});
res.status(200);
res.json(jsonArray);
}
} else {
res.json({error: err});
}
});
}
}
Put the console.log(statename) inside the function(err, states) call. Like this:
function(err, states){
states.forEach(function(statesLoop){
stateName = statesLoop.stateName;
});
console.log(stateName); //Should show something useful
});
Due the async nature of Node, the call to Mongo to find the state name was called, the console immediately printed the value (which was undefined), and then Mongo returned the results and presumably set the stateName variable.

Node.js Mongoose keeps adding same single element instead of all of them

In my code below, my value printed out at the console.log is correct, but when I search and go about entering the objects into the db, all the objects in the db contain the same hex, and image path but the id's are different. I tried first using findOne but the resulted in the same outcome. I am new to MongoDb so I am assuming it is just somethign I am doing stupid. Any ideas please send them my way :)
exports.addImage = function(req,res){
var params = req.body;
var colors = params.color;
var passedImg = params.path;
var ndxobj;
for(var index in colors){
ndxobj = colors[index];
//Values here are the correct index and contain valid data
console.log("col: ", ndxobj);
var query = clrModel.find({hex: ndxobj.hex}, function(err,result){
if(!err && result.length > 0){
console.log(result);
}else if(!err){
//We have an empty db for the searched obj
var locclr = new clrModel({
hex: ndxobj.hex
});
locclr.img.push({path:passedImg, date:ndxobj.imagedate});
locclr.save(function(error, data){
if(error){
console.log("Error in addImage find call: ",error);
res.json(error);
}
else{
console.log("Saving: ",data);
res.json(data);
}
});
}else {
//Handle error
}
});
}
};
I think that your paths are all the same because you set path to be passedImage, and passedImage is not updated from each index, but is set at the top of your code sample. As for the hex values being all the same, that seems to be happening because the callbacks are closing over ndxobj, so by the time they're called, all of them are looking at the same value. To make that work, you'll want to use a function to create your callbacks, something like what follows (hopefully I closed all my parens & brackets...). See this StackOverflow post for more info.
exports.addImage = function(req,res){
var makeCallback=function(ndxobj){
return function(err,result){
if(!err && result.length > 0){
console.log(result);
}else if(!err){
//We have an empty db for the searched obj
var locclr = new clrModel({
hex: ndxobj.hex
});
locclr.img.push({path:passedImg, date:ndxobj.imagedate});
locclr.save(function(error, data){
if(error){
console.log("Error in addImage find call: ",error);
res.json(error);
}else{
console.log("Saving: ",data);
res.json(data);
}
});
}else{
//Handle error
}
};
});
var params = req.body;
var colors = params.color;
var passedImg = params.path;
var ndxobj;
for(var index in colors){
ndxobj = colors[index];
//Values here are the correct index and contain valid data
console.log("col: ", ndxobj);
var query = clrModel.find({hex: ndxobj.hex}, makeCallback(ndxobj.hex));
}
};

Resources