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.