0

In my file, i am importing or requiring a file called mongoq.js which is as below:

var x;
var k;
var link;
  var Match=require('/home/gousia/Documents/livee/models/Match.js');
 function add(data,callback){
Match.matching.find({},function (err,mach){
    if(err) {
      return console.log(err);
    }
    k=mach.length;

   exports.k;
     if(mach.length){

    console.log(mach[0].toObject().link);
    x=mach[0].toObject().link;
  }

}).select({"link":1,"_id":0});
}
add("jjj",function(x)
{
if(k!=0)
  link=x;
 exports.link;
console.log("in query"+link);
});

I am requiring this file in a node module file called base64id.js, the code in the file looks like below:

Base64Id.prototype.generateId = function () {
var mon=require('./mongoq.js');

if(mon.k!=0)
{

  console.log("inb64"+mon.link);
  return mon.link;
}


if(mon.k==0)
{
  var rand = new Buffer(15); // multiple of 3 for base64
  if (!rand.writeInt32BE) {
    return Math.abs(Math.random() * Math.random() * Date.now() | 0).toString()
      + Math.abs(Math.random() * Math.random() * Date.now() | 0).toString();
  }
  this.sequenceNumber = (this.sequenceNumber + 1) | 0;
  rand.writeInt32BE(this.sequenceNumber, 11);
  if (crypto.randomBytes) {
    this.getRandomBytes(12).copy(rand);
  } else {
    // not secure for node 0.4
    [0, 4, 8].forEach(function(i) {
      rand.writeInt32BE(Math.random() * Math.pow(2, 32) | 0, i);
    });
  }

return rand.toString('base64').replace(/\+/g, '-').replace(/\//g, '_').replace(/\=/g,''); 
} 
};     

I exported two variables i.e., k and link from mongoq.js and i am trying to use them in base64id.js. But its not working as expected and returning undefined link. I think that the base64id.js file is continuing its execution before the imported file exports the variable. I tried using async(), wait.for() and promises to make it run synchronously but it doesnot work. Is there any other way to solve this problem? Please help.

Sai Kumar
  • 53
  • 1
  • 9
  • `I exported two variables i.e., k and link` - where? `exports.k` doesn't export jack – Jaromanda X Jun 14 '17 at 06:24
  • In mongoq.js i am exporting two variables and using it in base64id.js – Sai Kumar Jun 14 '17 at 06:26
  • `exports.k;` isn't using anything, it's a "do nothing" statement - you say you are exporting something from `mongoq.js` - well, you're not – Jaromanda X Jun 14 '17 at 06:28
  • I have already tried the solution explained on this question " How do I return the response from an asynchronous call? " but it doesnot worked for me thats why i have posted the question @Neil Lunn – Sai Kumar Jun 14 '17 at 06:30
  • also you declare `function add(data,callback){` - and you call it using `add('jjj', function(x) ...` ... but function `add` never calls the callback – Jaromanda X Jun 14 '17 at 06:31
  • Read it again and learn this time then. You are trying to set and interact with variables at "various places" in the code listed here. – Neil Lunn Jun 14 '17 at 06:31
  • I don't think there's any asynchronous code, is there? - unless `Match.matching.find` is – Jaromanda X Jun 14 '17 at 06:31
  • I have tried using exports.k=mach.length and exports.link=x but it too doesnot work @JaromandaX – Sai Kumar Jun 14 '17 at 06:33
  • that whole `Match.matching.find(` looks odd too, you have a callback in the find, then you chain a `select`, which you seem to do for no reason - and as already mentioned, add has a callback argument that is never used - it's all very strange - as for the *almost* correct use of `exports` - well, as the callback to `add('jjj'` is never called, it's no surprise that doesn't work, but, yeah, asynchronous code, you need to rethink how you're dealing with it – Jaromanda X Jun 14 '17 at 06:37
  • here's sort of how I'd expect to see this sort of code - https://jsfiddle.net/su0mh5fe/ - I say sort of, because it's still a mess – Jaromanda X Jun 14 '17 at 06:43
  • there's a better ref for asynchronous code - https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron – Jaromanda X Jun 14 '17 at 06:45

0 Answers0