I am encountering some problems with my convertKey function, and I suspect it is due to scope issues. Basically, I try to retrieve a record from my mongo database and store it in a count variable, but when I try to return it, I get "undefined". Surprisingly, console.log(nameSearch + count) works, while return nameSearch + count doesn't. Would really appreciate it if someone could help me on this!
var dbUrl = "kidstartnow",
collections = ["students", "studentsList"];
var db = require("mongojs").connect(dbUrl, collections);
function Student(name, src) {
this.name = name.toLowerCase();
//this function does not work
this.key = convertKey(this.name);
this.src = src;
this.pointsTotal = 0;
//inserts student into database
var student = {name: this.name, key: this.key, pointsTotal: this.pointsTotal,
src: this.src
};
db.students.insert(student);
//converts name to a key by stripping white space and adding a number behind and ensures keys are unique
//concatenates names together to form namesearch, and checks if entry exists in studentsList
function convertKey(name) {
var nameSearch = name.replace(/\s/g, ''),
count = 1;
db.studentsList.find({name: nameSearch}, function(err, student) {
//if nameSearch does not exist in studentsList, create entry and sets count to 1
if(err || !student.length) {
db.studentsList.insert({name: nameSearch, count: 1});
count = 1;
return nameSearch + count;
}
//if entry does exist, increments count by 1
else {
db.studentsList.update({name: nameSearch}, {$inc: {count: 1}}, function(err) {
if(err) {
console.log("Error incrementing records");
}
db.studentsList.find({name: nameSearch}, function(err, student) {
count = student[0].count;
//this works
console.log(nameSearch + count)
//but this doesn't
return nameSearch + count;
});
});
}
});
};
}
You're returning from the callback to db.studentsList.find and not from your convertKey function.
If you want the value to be returned from within db.studentsList.find, you'll need to either supply a callback to convertKey or possibly use a Promise library to make convertKey a deferred/future. Otherwise, your function will return immediately while waiting for your nested async functions to complete.
A callback allows you to pass on the result you're looking for (e.g. callback(nameSearch + count))
edit
Whenever I have questions about the return values of functions, I match braces with comments:
function convertKey(name) {
var nameSearch = name.replace(/\s/g, ''),
count = 1;
db.studentsList.find({name: nameSearch}, function(err, student) {
//if nameSearch does not exist in studentsList, create entry and sets count to 1
if(err || !student.length) {
db.studentsList.insert({name: nameSearch, count: 1});
count = 1;
return nameSearch + count;
} else {
db.studentsList.update({name: nameSearch}, {$inc: {count: 1}}, function(err) {
// ...
db.studentsList.find({name: nameSearch}, function(err, student) {
// ...
return nameSearch + count;
}); // end db.studentsList.find
}); // end db.studentsList.update
} // end else
}); // end db.studentsList.find
/**
* Notice, no return value here...
*/
}; // end convertKey
Related
i having Api Call which execute in For Loop some of the value which returns 10 sec itself some may take nearly 60 sec i have to maintain proper Timeout and clear session (i.e if results comes at 15 sec means it should goes to next input values and run the code) but currenly its waiting for 45 sec each single record how to optimize it
here my sample code :
if (selectedrows.length >= 1) {
for (var i = 0; i < selectedrows.length; i++) {
var myVar = setTimeout (function (k) {
var ob = { results: "Appending ..." };
child.update(selectedrows[k][4], selectedrows[k][4], ob);
var fullName = selectedrows[k][1] + ' ' + selectedrows[k][2];
math.ResultCall.async(fullName,function (err, res) {
if (err) throw err;
var returnedValue = JSON.parse(res);
console.log(returnedValue);
if(returnedValue.Result == null || returnedValue.Result.FOUND_Result == null)
{
console.log("None found")
}
else{
var obj = { results: “res” };
child.update(selectedrows[k][4], selectedrows[k][4], obj);
}
}
});
}, i * 45000,i);
}
}
Rephrasing your question, you need to return the data when your api gets resolved.
For this please go through https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/resolve
JavaScript, by default it work asynchronously because of its event loop.
You have promises and resolve to get notified when your api returns a data
Hope I helped :)
There are several approaches to implement the solution
1. Async-Await: in-case the records-processing order is important
for( let i=0; i<selectedrows.length; i++)
{
let ob = { results: "Appending ..." };
child.update(selectedrows[i][4], selectedrows[i][4], ob);
let fullName = selectedrows[i][1] + ' ' + selectedrows[i][2];
await new Promise((resolve,reject)=>
{
math.ResultCall.async(fullName,(err, res) => {
if (err) reject(err);
let returnedValue = JSON.parse(res);
console.log(returnedValue);
if(returnedValue.Result == null || returnedValue.Result.FOUND_Result == null) {
console.log("None found")
} else {
let obj = { results: “res” };
child.update(selectedrows[i][4], selectedrows[i][4], obj);
}
resolve();
});
}
**don't forget this means the wrapping function should be async as well (which returns a promise that can be resolved if necessary)
2.Promise.All: if the order is not important
let promArray = [];
for( let i=0; i<selectedrows.length; i++)
{
let ob = { results: "Appending ..." };
child.update(selectedrows[i][4], selectedrows[i][4], ob);
let fullName = selectedrows[i][1] + ' ' + selectedrows[i][2];
promArray.push( new Promise((resolve,reject)=>
{
math.ResultCall.async(fullName,(err, res) => {
if (err) reject(err);
let returnedValue = JSON.parse(res);
console.log(returnedValue);
if(returnedValue.Result == null || returnedValue.Result.FOUND_Result == null) {
console.log("None found")
} else {
let obj = { results: “res” };
child.update(selectedrows[i][4], selectedrows[i][4], obj);
}
resolve();
});
);
}
Promise.all(promArray);
** this will also return a Promise that can be resolved if necessary.
i have an array that holds student answers for given questions.
if a student gives an answer, it gets inserted into the array at the current index like answers[questionindex] = answer
later, i can read the array and map the entries to the given question-array
this case:
[
"answer",
undefined, // student has not given answer
"answer2",
]
works. (looping over the array, simply outputting "no answer given" if (answers[questionindex] === undefined)
but it doesn't work when the LAST answers were undefined (1 or more)
they just don't exist (of course).
how can i set those fields to undefined (like, after a timer reaches zero), to show that there was no answer given?
right now, the average-calculation shows 100% correct for 3 given (correctly), then 2 not given at all
code
var testResults = {
addRoom: function(Id, teacher) { // room pseudoconstructor
this[Id] = {
created: moment(),
runningProblem: false,
time: 0, // holds the countdown for the current problem
getTime: function() { // returns the countdown-counter
return this.time;
},
Id: Id,
teacher: teacher,
getCurrentSolution: function() {
return math.eval(this.testProblems[this.getCurrentProblemIndex()].problem);
},
getTimeTaken: function() {
return this.getCurrentProblemTimeLimit() - this.time;
},
getCurrentProblemTimeLimit: function() {
return this.testProblems[this.getCurrentProblemIndex()].timeLimit;
},
getCurrentProblemIndex: function() {
return this.testProblems.length - 1;
},
addTestProblem: function(problem, timeLimit) {
var solution = math.eval(problem);
this.testProblems.push({problem: problem, timeLimit: timeLimit, solution: solution});
console.dir(this.testProblems);
},
testProblems: [],
updatePercentages: function(name) {
function round(num) {
return +(Math.round(num + "e+2") + "e-2");
}
console.log('updating percentages');
console.log('answers length ' + this.students[name].givenAnswers.length);
var timeSum = 0;
for(var i = 0; i < this.students[name].givenAnswers.length; i++ ) {
timeSum += this.students[name].givenAnswers[i].takenTime;
}
var timeAvg = timeSum / this.students[name].givenAnswers.length;
console.log('timeAvg for ' + name + ' ' + timeAvg);
this.students[name].avgTime = round(timeAvg);
var correctSum = 0;
for(var j = 0; j < this.students[name].givenAnswers.length; j++ ) {
if (this.students[name].givenAnswers[j].correct) {
correctSum++;
}
}
var correctAvg = correctSum / this.students[name].givenAnswers.length;
console.log('correctAvg for ' + name + ' ' + correctAvg);
this.students[name].avgCorrect = round(correctAvg) * 100;
},
addGivenStudentAnswer: function(name, answer, takenTime, index) {
console.log('adding answer ' + name + ' ' +answer+ ' ' + takenTime);
var correct = this.getCurrentSolution() == answer;
if (typeof this.students[name].givenAnswers[index] === 'undefined') {
this.students[name].givenAnswers[index] = ({
answer: answer,
takenTime: takenTime,
correct: correct
});
this.updatePercentages(name);
//console.dir(this.students[name].givenAnswers);
return true;
} else {
console.log('attempt at double answer. not saved');
return false;
}
},
addStudent: function(name) {
if (!(this.students[name])) {
this.students[name] = {
studentName : name,
avgTime: 0,
avgCorrect: 0,
givenAnswers: []
}
}
console.dir(this);
},
students: {}
};
console.dir(this);
},
deleteRoom: function(Id) {
delete this[Id];
console.log('room deleted from testResults');
}
};
// after test
var name = socket.userName;
var room = socket.room;
var created = testResults[room].created;
var students = testResults[room].students;
var problems = testResults[room].testProblems;
var test = new tests({
roomId : room,
created : created,
teacher : name,
students : students,
problems : problems
});
test.save(function(err, result) {
if (err) {console.log(err);}
else {
console.log('test saved to DB');
socket.emit('testSaved');
// delete from roomList
testRooms.deleteRoom(room, name);
// delete from resultObject
testResults.deleteRoom(room);
// answer
io.in(room).emit('room Closed');
}
});
route for reading a test from DB afterwars
router.get('/showtests/:roomId', function(req, res) {
if (req.user && req.user.role === 'teacher') {
tests.findOne({roomId: req.params.roomId}, {}, function(err, result) {
if (err) {console.log(err);}
res.render('showSingleTest', {user: req.user, testData: JSON.parse(JSON.stringify(result))});
})
} else {
res.render('customerror', { title: "Error", errMsg1: "error.error", errMsg2: "error.notLoggedIn" });
}
});
aaaaaand the jade
h2(data-i18n="markup.studentsAnswers")
each student in testData.students
.testViewSingleStudentAnswers.col-md-6
h3 #{student.studentName}
ol.answers
each answer in student.givenAnswers
if (answer)
if (answer.correct == true)
li.correct
span #{answer.answer}
|
span.floatRight (#{answer.takenTime}s)
else
li.wrong
span #{answer.answer}
|
span.floatRight (#{answer.takenTime}s)
else
li.noAnswer(data-i18n="markup.noAnswerGiven")
.testTotals
| #{student.avgCorrect}
span(data-i18n="markup.percentCorrect")
| ,
| #{student.avgTime}
span(data-i18n="markup.avgTime")
You can do like so:
function push_answer(answer){
answer = answer || "undefined"
array_of_answers.push(answer)
}
Now, the value is not undefined, but it's defined by the literal. You can replace it with some unicode character in case some answer can be "undefined".
Have a nice day!
Seems to be working without issue for me.
HTML:
<div id="content">
</div>
JS:
var answers = ["answer1","answer2",undefined,"answer3",undefined];
for(i=0;i<answers.length;i++) {
if(!answers[i]){
answers[i]="no answer";
}
}
document.getElementById('content').innerHTML = answers;
jsFiddle
My personal recomendation: "Never leave things to chance"
if the student doesn't choose an answer, you should fill that blank space with a '', because undefined is really annoying to handle, so in order to fill the gaps:
This will check if answer is "undefined" and fill the gap with a '' (blank), then, when you check that answer, is going to be more simple to evaluate...
if(answer)
answers[questionindex] = answer;
else
answers[questionindex] = '';
I have list which is filled in for cycle where I calling assync function by thos way:
In For cycle I'm calling
row.SUCCES_RATE_SINCE = $scope.computeSuccessRateSinceStart(row);
Called function
// Calculate percentage of a whole from since
$scope.computeSuccessRateSinceStart = function(row) {
db = window.sqlitePlugin.openDatabase({name:"callplanner"});
// GET APPT COUNT
db.transaction(function(tx) {
tx.executeSql(sqlQuery, [], function(tx,results){
// init empty array for results
for (var i=0; i < results.rows.length; i++){
row = results.rows.item(i);
//Udpate date for writeout
//row.DATE = moment(row.DATE).format('ddd DD.M');
console.log("row APPT count is " + JSON.stringify(row));
apptCnt = row.APPT_CNT;
convCnt = row.CONVERS_CNT;
dailySuccessRateSince = apptCnt / convCnt * 100;
console.log("Success rate since is " +dailySuccessRateSince);
// THIS IS NOT WORKING
return Math.round(dailySuccessRateSince);
}
});
},function (e) {
console.log("ERROR: " + e.message);
$ionicLoading.show({
template: $translate.instant('ERROR_DATABASE'),
duration:1000
});
});
};
Problem is that computed value is always returned null (return function is executed before value is available in scope).
I'm quite new in Angular but i found that this issue could be solved using promises. Could somebody give me the example how to return value properly?
Many thanks for any help.
EDIT:
Called method is now triggered, but i cannot pass returned value into variable like this:
var test = $scope.computeSuccessRateSinceStart(row).then(function(result){
//ALERT WITH VALUE WORKS FINE
alert("Result " + result);
return result;
});
// THIS GIVES ME EMPTY ARRAY {}
alert("Result " + JSON.stringify(test));
Why don't you just make your method such that it always returns a promise, and then extract the result from the promise?
$scope.computeSuccessRateSinceStart = function(row) {
var deferred = $q.defer();
db = window.sqlitePlugin.openDatabase({name:"callplanner"});
// GET APPT COUNT
db.transaction(function(tx) {
tx.executeSql(sqlQuery, [], function(tx,results){
// init empty array for results
for (var i=0; i < results.rows.length; i++){
row = results.rows.item(i);
//Udpate date for writeout
//row.DATE = moment(row.DATE).format('ddd DD.M');
console.log("row APPT count is " + JSON.stringify(row));
apptCnt = row.APPT_CNT;
convCnt = row.CONVERS_CNT;
dailySuccessRateSince = apptCnt / convCnt * 100;
console.log("Success rate since is " +dailySuccessRateSince);
// THIS IS NOW WORKING:
deferred.resolve(Math.round(dailySuccessRateSince));
}
});
}, function(e) {
console.log("ERROR: " + e.message);
deferred.reject(e);
});
return deferred.promise;
};
Usage:
$scope.computeSuccessRateSinceStart(row).then(function(result){
// THIS GIVES THE VALUE:
alert("Result " + JSON.stringify(test));
return result;
}, function(e)
$ionicLoading.show({
template: $translate.instant('ERROR_DATABASE'),
duration:1000
});
});
In the book Node.js in Action, there is one example where files in directory "./text" is subject to word count. I want to ask if the closure used is actually necessarily or just a matter of style. The code is below:
var fs = require('fs');
var completedTasks = 0;
var tasks = [];
var wordCounts = {};
var filesDir = './text';
function checkIfComplete() {
completedTasks++;
if (completedTasks == tasks.length) {
for (var index in wordCounts) {
console.log(index + ': ' + wordCounts[index]);
}
}
}
function countWordsInText(text) {
var words = text
.toString()
.toLowerCase()
.split(/\W+/)
.sort()
for (var index in words) {
var word = words[index];
if (word) {
wordCounts[word] = (wordCounts[word]) ? wordCounts[word] + 1 : 1;
}
}
}
// My question is on the use of closure below
fs.readdir(filesDir, function(err, files) {
if (err) throw err;
for (var index in files) {
var task = (function(file) {
return function() {
fs.readFile(file, function(err, text) {
if (err) throw err;
countWordsInText(text);
checkIfComplete();
});
}
})(filesDir + '/' + files[index]);
tasks.push(task);
}
for (var task in tasks) {
tasks[task]();
}
});
This code is used in the book to demonstrate the nature of parallel control flow. My question is why does the code goes through this seemingly twisted elaboration (sorry, lol, a newbie here) of constructing closure then calling them? (I'm referring to the each task in tasks.)
Is that any different from what comes more natural to me like so?
fs.readdir(filesDir, function(err, files) {
if (err) throw err;
tasks = files; // Just to make sure the check for completion runs fine.
for (var index in files) {
var file = files[index];
fs.readFile(filesDir + '/' + file, function(err, text) {
if (err) throw err;
countWordsInText(text);
checkIfComplete();
});
}
});
Wouldn't it still be asynchronous and as "parallel" as it was in the previous code?
It is possible to avoid the closure here. The closure would be required to avoid the infamous closure-in-loop gotcha which would happen if the callback passed to fs.readFile accessed the loop variable or something derived from it. Because then, by the time the callback would be called, the value of the loop variable would be the last value assigned to it.
The way you do it is okay. I've not tried to completely clean the code or make it resilient to faults but I'd do:
var fs = require('fs');
var wordCounts = Object.create(null);
var filesDir = './text';
function countWordsInText(text) {
var words = text
.toString()
.toLowerCase()
.split(/\W+/)
.sort();
for (var index in words) {
var word = words[index];
if (word) {
wordCounts[word] = (wordCounts[word]) ? wordCounts[word] + 1 : 1;
}
}
}
fs.readdir(filesDir, function(err, files) {
if (err) throw err;
var total = files.length;
var completedTasks = 0;
for (var index in files) {
fs.readFile(filesDir + '/' + files[index], function(err, text) {
if (err) throw err;
countWordsInText(text);
completedTasks++;
if (completedTasks === total) {
for (var index in wordCounts) {
console.log(index + ': ' + wordCounts[index]);
}
}
});
}
});
I am using NodeJS to count the number of employees in different section. I am using Mongoose as ODM and MongoDB as database.That is my code (very simple for testing purposes).
exports.list= function( req, res){
var array = ['sectionA', 'sectionB'];
var i;
for(i = 0; i<array.length; i++){
Issue.count({ 'section.name': array[i]}, function (err, count) {
console.log('Section name is: ' + array[i] + ' number of employees: ' + count );
)};
}
}
But the value of array[i] is undefined inside Issue.count({ 'section.name': array[i]}, function (err, count) {});. But the value of count is absolutely right. I want an output like:
Section name is: sectionA number of employees: 50
Section name is: sectionB number of employees: 100
But my current output is
Section name is: undefined number of employees: 50
Section name is: undefined number of employees: 100
This is because value of i inside Issue.count({ 'section.name': array[i]}, function (err, count) {}); is always 2.
Is it possible that Issue.count function is asynchronous? So your loop is completing before the callback of:
function (err, count) {
console.log('Section name is: ' + array[i] + ' number of employees: ' + count );
}
is executed. When the callbacks are executed the value of i is undefined as a result.
#eshortie is correct: Issue.count is asynchronous and that's causing the problem.
Here's a solution:
for (i = 0; i<array.length; i++) {
Issue.count({ 'section.name': array[i]}, function(sectionName, err, count) {
console.log('Section name is: ' + sectionName + ' number of employees: ' + count );
}.bind(null, array[i]));
}
Don't try to execute asynchronous functions using a regular for loop. It is asking for problems. Use async.eachSeries or async.each instead https://github.com/caolan/async#eachseriesarr-iterator-callback
var async = require('async')
var Issue = {} // mongoose isue model here
var elements = ['foo', 'bar']
async.eachSeries(
elements,
function(name, cb) {
var query = {
'section.name': name
}
Issue.count(query, function(err, count) {
if (err) { return cb(err) }
console.dir(name, count)
})
},
function(err) {
if (err) {
console.dir(err)
}
console.log('done getting all counts')
}
)
Using Q library
var Q = require('q')
var i = 0;
function hello (item){
var defer = Q.defer();
Issue.count({'section.name': student}, function (err, count) {
if(err){
defer.reject(err);
}else{
var result = 'Section name is: ' + item + ' number of employees: ' + count ;
defer.resolve(result)
}
});
})
return defer.promise;
}
function recall(){
hello(checkItems[i]).then((val)=>{
console.log(val)
if(i < checkItems.length - 1){
i++
recall();
}
})
}
recall()