Mongoose, can't update model with for loop - javascript

I m doing a javascript function using mongoose to find a group which contains a list of users emails, that part works perfectly. After I want to find each user of the list and add the new group name instead of the old group name, I don't know why, but it doesn't work, the function returns me the group before doing the for loop, and the users are not modified.
Here is my user model :
var userSchema = mongoose.Schema({
local : {
email : String,
password : String,
}
groups : { type: [String], default: []}
});
And here my Group model :
var groupSchema = mongoose.Schema({
title : String,
creator : String,
listOfUsers : { type: [String], default: []}
});
And here my function :
Group.findOne({ '_id': groupId}, function (error, group){
var oldTitle = group.title;
group.title = req.body.title;
group.save();
if(error){
throw (error)
}
for(var i = 0; i < group.listOfUsers.length;i++){
User.findOne({'local.email': group.listOfUsers[i]}, function(error,user){
if(error){
throw (error)
}
for(var j=0;j<user.groups.length;j++){
if(user.groups[j] == oldTitle){
user.groups[j] = group.title;
user.save();
}
}
});
}
return res.json(group);

you can use async to fix the callback problem
async.waterfall(
[
function (callback) {
Group.findOne({
'_id': groupId
}, function (error, group) {
if (error) {
throw (error)
} else {
callback(null, group);
}
})
},
function (group, callback) {
for (var i = 0; i < group.listOfUsers.length; i++) {
User.findOne({
'local.email': group.listOfUsers[i]
}, function (error, user) {
if (error) {
throw (error)
} else {
callback(null, user, group);
}
});
}
},
function (user, group, callback) {
var oldTitle = group.title;
group.title = req.body.title;
for (var j = 0; j < user.groups.length; j++) {
if (user.groups[j] == oldTitle) {
user.groups[j] = group.title;
user.save();
}
}
callback(null, 'done');
}
],
function (err, result) {
console.info("4");
console.info(err);
console.info(result);
});
forgive me if i made any mistake, it is always hard to write code without data, i hope you will understand how i wanted to solve.
and don't forget
var async = require('async');
at the beginning

You should do user.save() only after for loop is finished. I updated for loops with forEach which I feel more comfortable to use.
Group.findbyId(groupId, function(err, group) {
var oldTitle = group.title;
group.title = req.body.title;
if (group.save()) {
listOfUsers.forEach(function(groupUser) {
User.findOne({'local.email' : groupUser}, function (err, user) {
if(user) {
user.group.forEach(function(userGroup) {
if (userGroup == oldTitle) {
userGroup = group.title;
}
})
user.save();
}
});
});
res.json(group);
} else {
res.status(400).json({messsage: 'Error while saving group!'});
}
});

Related

MongoDB query in for loop issue

I'm getting this array of user emails from the post data. I want to find the _id related to each email. I tried this for loop:
var studentIds = [];
for (var i = studentEmails.length - 1; i >= 0; i--) {
var email = studentEmails[i];
User.findOne({"email": email}, (err, student) => {
if (err) {
console.log("ERROR" + err);
}
if (student) {
var id = student._id;
studentIds.push(id);
console.log("STUDENT: " + student);
}
});
}
// Outside for loop
console.log('END');
However, this logs the following:
END
STUDENT: { _id: 5a11e667d7333203337cd9a4,
name: 'Patrick Jacobs',
email: 'windvaan#live.nl',
password: '$2a$10$CiSw/VH1HCaPtW6Sjz0X4.4avVoLsAH6iyF3FhidorahwLt1WDXoC',
__v: 0 }
STUDENT: { _id: 5a0f7dfb64b5a6000417c662,
name: 'Carlo Jacobs',
email: 'carlojacobs91#gmail.com',
password: '$2a$10$fiIosS4Jo5ehuCp3TfltSOnpypPMWSMvzlb7phRWmNGBtDz5W1rCG',
__v: 0 }
As you can see, the END is being printed first. I don't want that. I'm assuming the for loop is asynchronous? How can I make it synchronous?
Thx in advance!
The solution would be
var studentIds = [];
var loopRecords = function (records, cb) {
if (!records.length) return cb();
var email = records.shift();
User.findOne({"email": email}, (err, student) = > {
if (err) {
console.log("ERROR" + err);
}
if (student) {
var id = student._id;
studentIds.push(id);
console.log("STUDENT: " + student);
}
return loopRecords(records, cb);
}
}
loopRecords(studentEmails,function () {
console.log('END');
})
It is not the for loop, but the User.findOne call that is asynchronous.
Following this answer, you could use streamline.js; try the following to make the call synchronous.
var result = User.findOne({"email": email}, _);
if (result === null) {
console.log("ERROR" + err);
}
var id = result._id;
studentIds.push(id);
console.log("STUDENT: " + result);

Node js not saving method PUT

I am trying to update object values inside of array. I have done the method and search the element in the array that I want, and returns me 200. But after when I do another request the value return to the original (is not saving).
First of all this is the schema:
{
"_id" : "1",
"username" : "a",
"elements" : [{"_id": "22", "name":"bb"}, {"_id":"33", "name": "cc"}]
}
and this is my method
update = function(req, res) {
User.findById(req.params.id, function (err, user) {
if (!user) {
res.send(404, 'User not found');
}
else{
var array = user.elements;
for (var i = 0; i < array.length; i++) {
if (array[i]._id == "22") {
result = array[i];
if (req.body.name != null) result.name = req.body.name;
result.save(function(err) {
if(!err) {
console.log('Updated');
}
else {
console.log('ERROR: ' + err);
}
res.send(result);
});
break;
}
}
}
});
}
I don't know what I am doing wrong. I mean I simplified everything but I think that the problem is in the method.
You have to save the user object and result just like this :
update = function(req, res) {
User.findById(req.params.id, function (err, user) {
if (!user) {
res.send(404, 'User not found');
}
else{
var array = user.elements;
for (var i = 0; i < array.length; i++) {
if (array[i]._id == "22") {
result = array[i];
if (req.body.name != null) result.name = req.body.name;
break;
}
}
user.save(function(err) {
if(!err) {
console.log('Updated');
}
else {
console.log('ERROR: ' + err);
}
res.send(user);
});
}
});
}

Increment field of another collection in mongodb

I created two collections,one for enterprise and another for employees,their schema is as follows,
var mongoose= require('mongoose');
var Enterprise= new mongoose.Schema({
name:{type:String},
email:{type:String},
sector:{type:String},
employees: {type:Number,default:0}
});
module.exports={
Enterprise:Enterprise
};
var mongoose = require('mongoose');
var employee = new mongoose.Schema({
enterprise:{type: String},
name:{type:String},
email:{type:String},
password:{type:String},
gender:{type:String},
});
module.exports = {
employee:employee
};
my add employee route,
var mongoose = require('mongoose');
var q = require('q');
var employee = mongoose.model('employee');
var enterprise = mongoose.model('enterprise');
var addEmployee = function(req, res) {
newEmployee = new employee();
newEmployee.enterprise = req.params.enterprise;
newEmployee.name = req.params.name;
newEmployee.email = req.params.email;
newEmployee.gender = req.params.gender;
function detailSave() {
var deferred = q.defer();
newEmployee.save(function(err, data) {
if (err) {
res.send(500);
console.log('couldnt save employee details');
deferred.reject({errmessage: 'couldnt save employee details', err: err});
} else {
res.send(200);
deferred.resolve({data: data});
}
});
return deferred.promise;
}
function incrementEmployee(doc) {
var deferred = q.defer();
enterprise.findOneAndUpdate({ 'name': doc.enterprise }, { $inc: { 'employees': 1 } },
function(err, num) {
if (err) {
deferred.reject({errmessage: 'couldnt incrementEmployee', err: err});
res.send(500);
console.log('couldnt incrementEmployee');
} else {
res.send(200);
deferred.resolve({num:num});
}
});
return deferred.promise;
}
detailSave()
.then(incrementEmployee)
.then(function(success) {
console.log('success', success);
res.json(200, success);
})
.fail(function(err) {
res.json(500, err);
})
.done();
};
module.exports = {
addEmployee: addEmployee
};
The problem is when I add an employee, the employees field in enterprise collection doesn't increment
I think your query is not working since doc.enterprise is null
On the basis of your comment.
Try to give your query like this {'name': doc.data.enterprise}
function incrementEmployee(doc) {
var deferred = q.defer();
enterprise.findOneAndUpdate({
'name': doc.data.enterprise
}, {
$inc: {
'employees': 1
}
},
function(err, num) {
if (err) {
deferred.reject({
errmessage: 'couldnt incrementEmployee',
err: err
});
res.send(500);
console.log('couldnt incrementEmployee');
} else {
res.send(200);
deferred.resolve({
num: num
});
}
});
return deferred.promise;
}

cant add more than one object into db using mongoose

I have a function that is supposed to add new item each time there is a match as below:
function getTimeEntriesFromWorksnap(error, response, body) {
//console.log(response.statusCode);
var counter = 1;
if (!error && response.statusCode == 200) {
parser.parseString(body, function (err, results) {
var json_string = JSON.stringify(results.time_entries);
var timeEntries = JSON.parse(json_string);
_.forEach(timeEntries, function (timeEntry) {
_.forEach(timeEntry, function (item) {
Student.findOne({'worksnap.user.user_id': item.user_id[0]})
.populate('user')
.exec(function (err, student) {
if (err) {
throw err;
}
var newTimeEntry = _pushToObject(student.worksnap.timeEntries, item);
student.worksnap.timeEntries = {};
student.worksnap.timeEntries = newTimeEntry;
student.save(function (err) {
if (err) {
//return res.status(400).send({
// message: errorHandler.getErrorMessage(err)
//});
} else {
//res.json(item);
}
});
});
});
});
});
}
}
For some reason it is only inserting once for each student that it finds.
And my Student schema looks like this:
var StudentSchema = new Schema({
firstName: {
type: String,
trim: true,
default: ''
//validate: [validateLocalStrategyProperty, 'Please fill in your first name']
},
lastName: {
type: String,
trim: true,
default: ''
//validate: [validateLocalStrategyProperty, 'Please fill in your last name']
},
worksnap: {
user: {
type: Object
},
timeEntries: {
type: Object
},
}
});
Any solution?
My Guess is its always pushing the last one... closures...
function getTimeEntriesFromWorksnap(error, response, body) {
//console.log(response.statusCode);
var counter = 1;
if (!error && response.statusCode == 200) {
parser.parseString(body, function (err, results) {
var json_string = JSON.stringify(results.time_entries);
var timeEntries = JSON.parse(json_string);
_.forEach(timeEntries, function (timeEntry) {
_.forEach(timeEntry, function (item) {
saveStudent(item);
});
});
});
}
}
Below is saveStudent function
function saveStudent(item) {
Student.findOne({
'worksnap.user.user_id': item.user_id[0]
})
.populate('user')
.exec(function(err, student) {
if (err) {
throw err;
}
var newTimeEntry = _pushToObject(student.worksnap.timeEntries, item);
student.worksnap.timeEntries = {};
student.worksnap.timeEntries = newTimeEntry;
student.save(function(err) {
if (err) {
//return res.status(400).send({
// message: errorHandler.getErrorMessage(err)
//});
} else {
//res.json(item);
}
});
});
}
OR
wrap it inside a closure...
function getTimeEntriesFromWorksnap(error, response, body) {
//console.log(response.statusCode);
var counter = 1;
if (!error && response.statusCode == 200) {
parser.parseString(body, function(err, results) {
var json_string = JSON.stringify(results.time_entries);
var timeEntries = JSON.parse(json_string);
_.forEach(timeEntries, function(timeEntry) {
_.forEach(timeEntry, function(item) {
(function(item){
Student.findOne({
'worksnap.user.user_id': item.user_id[0]
})
.populate('user')
.exec(function(err, student) {
if (err) {
throw err;
}
var newTimeEntry = _pushToObject(student.worksnap.timeEntries, item);
student.worksnap.timeEntries = {};
student.worksnap.timeEntries = newTimeEntry;
student.save(function(err) {
if (err) {
//return res.status(400).send({
// message: errorHandler.getErrorMessage(err)
//});
} else {
//res.json(item);
}
});
});
}(item))
});
});
});
}
}
Can you use async library and check out...
var async = require('async');
async.map(timeEntries, function (timeEntry, next) {
async.map(timeEntry, function (item, next) {
//your code.
});
});

Cleaner code to save array data with mongoose

I created a function to save data into mongoDB with logic below, but I really have difficulty to refactor the code and make it cleaner, there are so many annoying code duplication, how can I have DRY principle?
Logic:
1. pass in a flag to decide either close DB connection or not at end.
2. create different mongoDB models according to the passed in returnedArray and save into DB.
var saveArrayToDB = function(returnedArray, flagToCloseDBConnection) {
var objectToSave,
object,
type = returnedArray[0].type,
arrayToSave = [];
if (type === 'user') {
for (var i = 0; i < returnedArray.length; i++) {
object = returnedArray[i];
objectToSave = new User({
fullName: object['full_name'],
activatedAt: object['activated_at'],
location: object['location'],
timezone: object['timezone'],
imageURL: object['mugshot_url'],
stats: object['stats']
});
arrayToSave.push(objectToSave);
}
User.create(arrayToSave, function(err) {
if (err) {
console.log('err ' + err);
}
if(flagToCloseDBConnection) {
mongoose.connection.close();
}
});
} else if (type === 'group') {
for (var j = 0; j < returnedArray.length; j++) {
object = returnedArray[j];
objectToSave = new Group({
fullName: object['full_name'],
createdAt: object['created_at'],
stats: object['stats'],
url: object['web_url']
});
arrayToSave.push(objectToSave);
}
Group.create(arrayToSave, function(err) {
if (err) {
console.log('err ' + err);
}
if(flagToCloseDBConnection) {
mongoose.connection.close();
}
});
} else {
objectToSave = null;
console.log('ERROR: unrecognized type in data. Not saved.');
}
};
Just to add on to what #JohnnyHK commented on your question, it would be best if you keep your mongoose connection open during your application lifecycle. Other than that you could use some JavaScript functions like map() to initialize the arrays, define common callback functions that you can reuse in both create and map methods:
var saveArrayToDB = function(returnedArray, flagToCloseDBConnection) {
var type = returnedArray[0].type,
arrayToSave = [];
var callback = function(err) {
if (err) { console.log('err ' + err); }
};
var newUser = function(u){
return new User({
fullName: u['full_name'],
activatedAt: u['activated_at'],
location: u['location'],
timezone: u['timezone'],
imageURL: u['mugshot_url'],
stats: u['stats']
});
};
var newGroup = function(g){
return new Group({
fullName: g['full_name'],
createdAt: g['created_at'],
stats: g['stats'],
url: g['web_url']
});
};
if (type === 'user') {
arrayToSave = returnedArray.map(newUser);
User.create(arrayToSave, callback);
} else if (type === 'group') {
arrayToSave = returnedArray.map(newGroup);
Group.create(arrayToSave, callback);
} else {
console.log('ERROR: unrecognized type in data. Not saved.');
}
};
No need for closing the connection. Here's an already much improved version:
var factories = {
'user': {
method: function(object){
return {
fullName: object['full_name'],
activatedAt: object['activated_at'],
location: object['location'],
timezone: object['timezone'],
imageURL: object['mugshot_url'],
stats: object['stats']
};
},
model: User
},
'group': {
method: function(object){
return {
fullName: object['full_name'],
createdAt: object['created_at'],
stats: object['stats'],
url: object['web_url']
};
},
model: Group
}
}
var saveArrayToDB = function(returnedArray) {
var saveQueue=[],
factory = factories[returnedArray[0].type];
if(!factory){
return console.log('ERROR: unrecognized type in data. Not saved.');
}
returnedArray.forEach(function(item){
saveQueue.push(factory.method(item));
});
factory.model.create(saveQueue, function(err){
if(err){
console.log('err ' + err);
}
});
};
(You don't need to pass document instances, plain objects are good enough for Model.create)

Categories

Resources