一旦我运行代码并发送交易报价(接收项目,获取数据,然后在获取数据后我的应用程序采取此步骤)我收到错误。
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
我试图用if(roundItemsData !== null || roundItemsData !== undefined)
找到解决方法,但它似乎不起作用......
有任何想法吗?我是节点和猫鼬的新手。
请注意,第一次初始化此部件时,它尚未设置roundCode。
答案 0 :(得分:1)
我认为你只是混淆了&amp;&amp; (AND)与|| (或) - 这应该有效:
if(roundItemsData !== null && roundItemsData !== undefined) {
通常我们只写:
if (roundItemsData) {
这几乎是相同的,但更短。