javascript - 使用 Mongoose 对数据进行的操作不起作用

标签 javascript node.js mongodb 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:无法读取未定义的属性“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/

相关文章:

javascript - 无法将所有整数日期转换为字符串

javascript - 如何在 node.js 中将 HTTP 响应主体编码为 UTF-8

javascript - 如何使用 jquery 将文本保存在图像上

javascript - Node http-proxy-middleware 不能将本地服务器作为目标

node.js - 启动 mongodb 时出现 TransientTransactionError

node.js - 如何根据更改的文件修改 grunt watch 任务?

mongodb - mongo.Connect() 在使用 mongo-go-driver 的 Go 中无法按预期工作

mongodb - spring data - Mongodb - findBy 嵌套对象的方法

MongoDB 更改流在插入时返回空 fullDocument

javascript - 改变选项(选择)元素的值?