一旦我运行代码并发送交易报价(接收商品,获取数据,然后在获取数据后我的应用程序执行此步骤),我就会收到错误。
emitter.on('depositImportedToDb', function(createNewDeposit) {
var roundCode;
var roundItems;
var roundItemsData;
emitter.on('updateItemsData', function(roundCode) {
Round.findOne({ roundCode: roundCode }, function(err, data) {
if (err) console.error(err);
roundItemsData = data;
console.log(roundItemsData);
});
if(roundItemsData !== null || roundItemsData !== undefined) {
for(var i = 0; i < roundItemsData.deposit.length; i++) {
roundItems = roundItems + roundItemsData.deposit[i].items.length;
}
console.log("Current number of items inside a round " + roundItems);
}
});
emitter.on('createNewRound', function() {
roundCode = crypto.randomBytes(15).toString('hex');
var round = new Round({
roundCode: roundCode
});
round.save(function(err, round) {
if (err) return console.error(err);
else {
console.log("Created new Round");
}
});
Round.update({ "roundCode" : roundCode }, {$push: {deposit: createNewDeposit}}, function(err, data) {
if (err) console.error(err);
else console.log("Succesfuly inserted new deposit into round");
});
emitter.emit('updateItemsData', roundCode);
});
emitter.on('getWinner', function(roundCode) {
// ------ TO DO !!! --------
});
emitter.on('addDataToRound', function(createNewDeposit) {
Round.update({ "roundCode" : roundCode }, {$push: {deposit: createNewDeposit}}, function(err, data) {
if (err) console.error(err);
else console.log("Succesfuly inserted new deposit into round");
});
emitter.emit('updateItemsData', roundCode);
});
if(roundCode === undefined || roundCode === null) {
emitter.emit('createNewRound');
} else if (createNewDeposit.items.length + roundItems >= 30){
console.log ("Too many items, selecting winner, actualy numeber: " + roundItems);
emitter.emit('getWinner', roundCode);
} else if (createNewDeposit.items.length + roundItems <= 30 && roundCode === undefined && roundCode === null ) {
emitter.emit('addDataToRound', createNewDeposit);
console.log('Adding items to the round, less than 30 items total and roundCode valid');
}
});
我得到的错误是:TypeError:无法读取未定义的属性“deposit”
我尝试使用 if(roundItemsData !== null || roundItemsData !== undefined)
找到解决方法,但它似乎不起作用......
有任何想法吗?我对 Node 和 Mongoose 有点陌生。
请注意,第一次初始化此部分时,尚未设置 roundCode。
最佳答案
我认为您只是将 && (AND) 与 || 混淆了(或)- 这应该有效:
if(roundItemsData !== null && roundItemsData !== undefined) {
通常我们只是写:
if (roundItemsData) {
这(几乎)相同,但要短得多。
关于javascript - 使用 Mongoose 对数据进行的操作不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33766437/