cant add more than one object into db using mongoose - javascript

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.
});
});

Related

How to make the program wait for the if statement to finish before continuing in javascript?

I'm new to Javascript. I want to make this block run after the if statement is finished (asynchronous). The reason I want that is that I want to make some changes to update them if it falls into the if statement
let params = {
TableName: "storepedia-test",
Item: updatedItem
};
docClient.put(params, function (err, data) {
if (err) {
console.log(err);
} else {
res.redirect('/devices');
}
});
Here is my whole code
const { id } = req.params;
const file = req.file;
let updatedItem = { ...req.body};
updatedItem.id = id;
if (file !== undefined){
const deleteParams = {
Key: updatedItem.image,
Bucket: bucketName
}
s3.deleteObject(deleteParams, async (err, data) => {
if (err) {
console.log(err)
} else {
const result = await uploadFile(file);
console.log('result', result);
await unlinkFile(file.path);
updatedItem.image = result.Key;
let params = {
TableName: "storepedia-test",
Item: updatedItem
};
docClient.put(params, function (err, data) {
if (err) {
console.log(err);
} else {
res.redirect('/devices');
}
});
}
})
}
let params = {
TableName: "storepedia-test",
Item: updatedItem
};
docClient.put(params, function (err, data) {
if (err) {
console.log(err);
} else {
res.redirect('/devices');
}
});
Just to run something after the if? I think this is the best spot:
docClient.put(params, function(err, data) {
if (err) {
console.log(err);
} else {
// run async code here.
// when done do the redirect.
// for example:
s3.do_something(function(err, data) {
if (err) {
console.log(err)
} else {
console.log(data)
res.redirect('/devices');
}
})
}
});

How to use the equality operator correctly in Nodejs

I want to get all the flights of the airline_name and destination entred, but I get all flights of all airlines and all destinations not of the specified entries. What should I change in the code so it can work?
function get_flights_by_airline_destination(airline_name, destination, callback) {
let request = require('request');
let url = 'http://api.aviationstack.com/v1/flights?access_key=xxxx&flight_status=scheduled'
request(url, function (err, response, body) {
if (err) {
console.log('error:', error);
callback(err);
} else {
let vol = JSON.parse(body)
result= []
num = vol.pagination.limit
for (let i = 0; i < 10; i++) {
arrival = vol.data[i].arrival.timezone
airline=vol.data[i].airline.name
if ((destination == arrival)&&(airline_name == airline)) {
one_flight = {number: vol.data[i].flight.number, date: vol.data[i].flight_date, departure: vol.data[i].departure.timezone, arrival: vol.data[i].arrival.timezone, from: vol.data[i].departure.airport, to: vol.data[i].arrival.airport, airline: vol.data[i].airline.name}
result.push(one_flight)
}
}
callback(null, result)
}
});
}
function get_flights( req, res) {
get_flights_by_airline_destination( 'air france','europe/rome' ,function( err, result) {
if ( err) {
console.log("error")
}
else {
var links = []
result.map( ( result) => {
links.push(
'from '+' '+result["departure"]+' '+ 'to'+' '+result["arrival"]+' '+'on'+' '+result["airline"],
)
})
console.log(links)
}
})
}
get_flights()

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;
}

Mongoose, can't update model with for loop

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!'});
}
});

Unit testing a method that calls other methods

I am new to testing. I am trying to unit-test a method that calls 2 other methods. I tried stubbing those 2 methods, but it looks like the original methods are still getting called. I am trying to test whether my method adds the object the savePoll method returns to the user.polls array.
test.js
var UserStub = sinon.stub();
var PollStub = sinon.stub();
var OptionStub = sinon.stub();
var saveOptionsStub = sinon.stub();
var savePollStub = sinon.stub();
var users = proxyquire('../controllers/users', {
'../models/user': UserStub,
'../models/poll': PollStub,
'../models/option': OptionStub
});
beforeEach(function() {
res = {
json: sinon.spy(),
send: sinon.spy(),
status: function(responseStatus) {
return this;
}
};
req = {
params: {
id: 1
}
};
UserStub.save = function(callback) {
callback(null, req.body);
};
});
describe('add a poll', function() {
it('should add the poll', function() {
req.body = {
name: 'Poll1',
options: [{
text: 'option1',
votes: 0
}, {
text: 'option2',
votes: 1
}]
};
var user = {};
user.polls = [];
UserStub.findById = function(query, callback) {
callback(null, user);
};
sinon.stub(require('../controllers/users'), 'saveOptions').returns([OptionStub, OptionStub]);
sinon.stub(require('../controllers/users'), 'savePoll').returns({});
users.addPoll(req, res);
expect(user.polls).to.equal('[{}]');
});
});
users.js
addPoll: function(req, res) {
var self = this;
User.findById(req.params.user_id, function(err, user) {
if (err) return res.status(400).json(err);
self.saveOptions(req.body.options)
.then(function(optionsArr) {
var pollName = req.body.name;
self.savePoll(pollName, optionsArr)
.then(function(poll) {
user.polls.push(poll);
user.save(function(err, user) {
if (err) return res.status(500).json(err);
res.json(poll);
});
}, function(err) {
return res.status(500).json(err);
});
}, function(err) {
return res.status(400).json(err);
});
});
},
savePoll: function(pollName, optionsArr) {
return new Promise(function(resolve, reject) {
var poll = new Poll();
poll.name = pollName;
poll.options = optionsArr.slice();
poll.save(function(err, poll) {
if (err) {
reject(err);
} else {
resolve(poll);
}
});
});
},
saveOptions: function(options) {
var optsArr = [];
var promises = options.map(function(opt) {
return new Promise(function(resolve, reject) {
var option = new Option(opt);
return option.save(function(err, option) {
if (err) {
reject(Error(err));
} else {
resolve(optsArr.push(option));
}
});
});
});
return Promise.all(promises).then(function() {
return optsArr;
}).catch(function(err) {
console.log(err);
});
}

Categories

Resources