NotAJohnDoe
NotAJohnDoe

Reputation: 125

Operations on data using mongoose not working

Once I run the code and send the trade offer (receive item, get data, then after getting data my app takes this step) I get an error.

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');
    }
});

The Error i get is : TypeError: Cannot read property 'deposit' of undefined I tried to find a way around that with if(roundItemsData !== null || roundItemsData !== undefined) but it doesn't seem to work... Any ideas? I'm kinda new to node and mongoose.

Please note that first time this part is being initialized it doesn't have roundCode set yet.

Upvotes: 1

Views: 22

Answers (1)

Reto
Reto

Reputation: 3141

I think you simply confused && (AND) with || (OR) - this should work:

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

often we just write:

if (roundItemsData) {

this is (almost) the same, but much shorter.

Upvotes: 1

Related Questions