使用mongoose的数据操作不起作用

一旦我运行代码并发送交易报价(接收项目,获取数据,然后获取数据后,我的应用程序采取这一步),我得到一个错误。

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: Cannot read property 'deposit' of undefined我试图find一种方式与if(roundItemsData !== null || roundItemsData !== undefined)但它似乎并没有工作.. 。 有任何想法吗? 我对节点和mongoose有点新鲜感。

请注意,第一次这个部分正在初始化,它没有设置roundCode。

我想你简单地把&&(AND)与||混淆了 (OR) – 这应该工作:

 if(roundItemsData !== null && roundItemsData !== undefined) { 

我们经常只写:

 if (roundItemsData) { 

这是(几乎)相同,但更短。