我正在处理一个保存到 mongodb 数据库存储的 javascript(node.js) 示例。
我遇到了一段代码,我不太明白本教程的作者到底想做什么以及为什么。这只是我缺乏理解,但我真的很想知道这里发生了什么。请参阅下面的代码以及我在相关行旁边的评论:
//save new employee
EmployeeProvider.prototype.save = function(employees, callback) {
this.getCollection(function(error, employee_collection) {
if( error ) callback(error)
else {
// This is the portion I have a question on.
// So this is checking to see if the array.length is undefined? Why would the length property be undefined?
if( typeof(employees.length)=="undefined")
// What is going on here exactly?
// It looks like he is initializing and array with employees parameter that was passed in to the save
// function earlier? I've never seen this kind of thing done before. Thoughts?
employees = [employees];
for( var i =0;i< employees.length;i++ ) {
employee = employees[i];
employee.created_at = new Date();
}
employee_collection.insert(employees, function() {
callback(null, employees);
});
}
});
};
谢谢你的帮助!
克里斯