I'm trying to remove the lowest value from each student with this code i have made so far:
// Find smallest value
Array.prototype.min = function() {
return Math.min.apply(null, this);
};
var MongoClient = require('mongodb').MongoClient;
MongoClient.connect('mongodb://localhost:27017/school', function(err, db) {
if(err) throw err;
var collection = db.collection('students');
var cursor = collection.find();
cursor.each(function(err, docs){
if(err) throw err;
if(docs == null) throw err;
var student = docs;
var studentHWScores = [];
console.log(" ");
for (var i = 0; i < docs.scores.length; i++) {
var score = docs.scores[i];
if (score.type == 'homework') {
studentHWScores.push(score.score);
};
};
var studentId = student._id.toString();
var lowestScore = studentHWScores.min().toString();
// If equal 2 or more in grades remove lowest
//console.log("studentHWScores.length >= 2: ", studentHWScores.length >= 2);
if(studentHWScores.length >= 2) {
collection.update({
'_id': studentId
}, {
'$pull': {
'scores': {
'score': lowestScore
}
}
}, function(err, updated) {
if(err) throw err;
console.log("updated: ", updated);
});
}
console.log(" ");
});
});
I get the following error when im trying to run it: (i also get a lot of white space and then (updated: 0) trough my console.log
/Users/sp/Documents/dev/felicity/node_modules/mongodb/lib/mongodb/connection/server.js:500
throw err;
^
TypeError: Cannot read property 'err' of null
at Object.toError (/Users/sp/Documents/dev/felicity/node_modules/mongodb/lib/mongodb/utils.js:113:18)
at Server.Base._callHandler (/Users/sp/Documents/dev/felicity/node_modules/mongodb/lib/mongodb/connection/base.js:455:65)
at /Users/sp/Documents/dev/felicity/node_modules/mongodb/lib/mongodb/connection/server.js:487:18
at MongoReply.parseBody (/Users/sp/Documents/dev/felicity/node_modules/mongodb/lib/mongodb/responses/mongo_reply.js:68:5)
at null.<anonymous> (/Users/sp/Documents/dev/felicity/node_modules/mongodb/lib/mongodb/connection/server.js:445:20)
at emit (events.js:95:17)
at null.<anonymous> (/Users/sp/Documents/dev/felicity/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:207:13)
at emit (events.js:98:17)
at Socket.<anonymous> (/Users/sp/Documents/dev/felicity/node_modules/mongodb/lib/mongodb/connection/connection.js:440:22)
at Socket.emit (events.js:95:17)
Aucun commentaire:
Enregistrer un commentaire