0

I've got problem. I can't set a response variable to var. I am using node.js + mongo.db That's how my code looks:

function updateTradeLink(sid, link) {
    if(!g_Mongoconnected)
        return 0;
var TradeUrl = '';
existUser(sid, function(exist){
if(exist)
    {
        userListDB.find({'steamid':sid}).toArray(function(err, list) { 
        TradeUrl = list.trade-link;
        });
        helper.log(TradeUrl);
        var PartnerURL = TradeUrl.substring(0,60);
        var linkU = link.substring(0,60);
        helper.log(linkU);
        helper.log(PartnerURL);
        if (PartnerURL == linkU)
        {
            userListDB.update({steamid: sid}, {steamid: sid, tradelink: link});
        }
        else
        {
            helper.log('detected tried to change partner!');
        }
    }
else userListDB.insert({steamid: sid, tradelink: link}, {w:1}, function(err)
    {
        if(err) 
        {
            helper.log('Error inserting tradelink', 485, err);
        }
    });
});

}

And its not giving any response, my database looks: http://prntscr.com/7v0yx8

Seebix
  • 13
  • 2
  • 7
  • Try handling the error in the callback from `.find()`. If that doesn't give you a hint you should look else where in your source, as there could be a fluke somewhere else. If you want a better answer try providing some more details. – tsturzl Jul 20 '15 at 22:31
  • Did if(err) { helper.msg("error"); } nothing popped out, so everythings is okay. So why is it happening? – Seebix Jul 20 '15 at 22:35
  • Ah, missed it the first time. You have a misunderstanding of node.js event driven nature – tsturzl Jul 20 '15 at 22:39
  • The callback for `find()` is likely firing after you've tried to log TradeUrl. – tsturzl Jul 20 '15 at 22:41
  • Ben's thing isnt working, giving undefined as variable. – Seebix Jul 20 '15 at 22:43

1 Answers1

0

find() is asynchronous. The update, therefore, should be in find()'s callback. Here is the revised code:

function updateTradeLink(sid, link) {
  if (!g_Mongoconnected) {
    return 0;

  }
  var TradeUrl = '';
  existUser(sid, function(exist) {
    if (exist) {
        userListDB.find({'steamid': sid}).toArray(function(err, list) {
            TradeUrl = list['trade-link'];

            helper.log(TradeUrl);
            var PartnerURL = TradeUrl.substring(0, 60);
            var linkU = link.substring(0, 60);
            helper.log(linkU);
            helper.log(PartnerURL);
            if (PartnerURL == linkU) {
                userListDB.update({steamid: sid}, {steamid: sid, tradelink: link});
            }
            else {
                helper.log('detected tried to change partner!');
            }
        });
    } else {
        userListDB.insert({steamid: sid, tradelink: link}, {w: 1}, function(err) {
            if (err) {
                helper.log('Error inserting tradelink', 485, err);
            }
        });
    }
  });
}

Note: TradeUrl should be checked before doing TradeUrl.substring(0, 60). In addition: list returned by find() is really an array, so list['trade-link'] might not be the right thing to do.

Ben
  • 5,024
  • 2
  • 18
  • 23
  • Hello Ben, thanks for your answer. It stil gives me a variable as "undefined" What can I do? – Seebix Jul 20 '15 at 22:41
  • you need to reference 'trade-link' as list['trade-link'], not list.trade-link. If it's still undefined, then print out 'list' (result of find()) to see what's in it – Ben Jul 20 '15 at 22:44
  • Did it, I just copied ur code and it happens. Output shows object object: http://prntscr.com/7v179g – Seebix Jul 20 '15 at 22:46
  • console.log(list) doesn't print the json object, try console.log("%j", list) – Ben Jul 21 '15 at 03:34