【发布时间】:2016-05-31 09:07:22
【问题描述】:
我今天编写了这段代码,但我不知道有什么问题?理论上,findOne() 函数应该为 for() 的每个循环工作。这不是发生的事情。在数组中有几个元素,我想保存所有这些。我是 NodeJS 的新手,但是 for() 的循环总是一样的,还是不一样?
var sType = models.Type; // Model Mongoose
var word = ''; // Word
var i = 0;
for( i = 0; i < arrayData.length; i++ ){
word = arrayData[i]; // Save the element in word, I made this to try to pass the variable.
sType.findOne({ nameType: word }, { _id : 1 }, function( err, type ){
console.log( word ); // In the console, show me the last element of array x20.
if( err )
throw err;
if( !err && type == null ){
var types = new sType({
nameType: word
});
types.save( function( err ){
if( err )
throw err;
});
}
});
}
如果我不创建变量 'word',程序会抛出一个错误'Validation Error: Path 'nameTYpe' is required。'
【问题讨论】:
标签: arrays node.js mongodb mongoose