1

Function checkExists is taking too long to execute. Tried to use await async function but had no effect. var exists = await checkExists(data.email); is returning undefined because not awaiting for checkExists.

I have my index.js:

const express = require('express');
const app = express();

require('./private/signUpAPI')(app);

app.listen(80, () => console.log('listening on 80'));
app.use(express.static('public'));
app.use(express.json({limit: '1mb'}));

And my signUpAPI.js:

const DataStore = require('nedb');
const express = require('express');

const database = new DataStore('private/database.db');
database.loadDatabase();

module.exports = function api(app){
    app.use(express.json({limit: '1mb'}));
    app.post('/signUpAPI', async (request, response) => {
        console.log("Sign Up Request received!");
        const data = request.body;

        var exists = await checkExists(data.email);

        console.log(exists)
        console.log(data);
        console.log("Added to DB");
        console.log('-------------------------' + '\n');
        database.insert(data);
        const testData = {"status": "success"};
        response.send(testData);
    });    
}

async function checkExists(email){
    var exists = false;

    database.find({"email": email}, async function(err, docs){
        if (docs.length > 0){exists = true;}
        console.log(docs.length);
        return exists;
    });
}

this is the node output when running index.js and calling the fetch('/signUpAPI'):

Sign Up Request received!
undefined
{
  email: 'a',
  username: 'a',
  hashPass: 'da180265625ebeaf62f4ee1813bdc28faeaf79f0b2b329290758a1c095111ae8',
  salt: 'g8VkTBV$+Bh35K9ns7Zt*9^CH#M=VELSzKUX=H3^+5kpFV=bEbVfXFtF*GGYHOa#'
}
Added to DB
-------------------------

37

I currently have 37 entries in the DB with the same data hence the console.log(docs.length) returning 37.

But this is executing last and appears at the bottom of the console when it should appear at the top.

Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107
Gullen
  • 13
  • 4

2 Answers2

3

Use https://www.npmjs.com/package/nedb-promise

so you can use await for database queries and you can change your code like this -

async function checkExists(email) {
    const record = await database.findOne({ email });
    console.log(record);
    if (record) return true;
    return false;
}
Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107
2

Functions you want to wait should return a promise in order to wait for response. You either resolve the promise if the operation result is success or reject with an error.

The flow should like something like this;

async function func1()
{
    try
    {
        var tmp = await func2();
        console.log(tmp);
    }
    catch(err)
    {
        console.log(err);
    } 
}

async funcion func2()
{
    return new Promise(async function (resolve, reject)
    {
        if(true)
        {
            resolve("success");
        }
        else
        {
            reject("error");
        }
    });
}
Eagleclaw
  • 369
  • 1
  • 13