0

I've been having trouble with handling data from MongoDB using MongoJS where I have a promise that refuses to resolve. I am honestly confused why this does not work. It works completely fine on my local machine but once I move it to my server it breaks. Except it only breaks as a module. Is it because I'm using deasync? If it is, how do I fix it?

Here's what I'm doing compressed down to what doesn't work for me

In the index:

const db = require('./db')
//Obviously this works I'm just showing what I mean by "module"

data = db.get('somedata') //Passthrough for getData
console.log(data)
//On local machine, console says: thedata
//On deb server, console says: Promise { 'thedata' }
//On deb server, console ALSO says: MongoDB request timed out: Promise { 'thedata' }
// ^ (this is from the timeout part I added because it froze)

In the DB module:

const db = mongojs(`connectionURIstring`, ['bot'])
const bdata = db.bot

function syncP(promise) {
    var answer;
    promise.then(value => {
        //Runs every time on local machine but not on server as module
        answer = value
    })
    //I also have .catch but it's not relevant
    setTimeout(function () {
        //Never runs on local machine but runs every time on server as module
        if (answer) return;
        answer = promise
        console.error('MongoDB request timed out:', promise)
    }, 2000)
    require('deasync').loopWhile(function () { return !answer })
    return answer
}

function getData(datapath) {
    //Datapath processing here
    var GDP = new Promise((resolve, reject) => {
        bdata.findOne({
            _id: primary
        }, function (err, dataJ) {
            //Stuff where I format data, comes out as variable data
            resolve(data)
        })
    })
    return syncP(GDP)
}

I genuinely don't know why this doesn't work. There's no reason it shouldn't work as a module if it works independently. It works on another computer. The only thing I can assume is that it's something to do with how deasync works, but even then, there's no reason why that would interfere with the promise, as it's separate from the promise itself.

This is driving me insane.

10Nates
  • 102
  • 10

0 Answers0