Categories
Development MongoDB

Value retrieved from MongoDB is undefined when fetched by Express.js server

Basically what I’m trying to accomplish is pulling the data from a MongoDB database and feed it into my Express.js server. Both of these are ran on the localhost with different addresses. I have tried numerous different methods to get it working and can’t seem to get it to work.

Express portion

    app.get('/products', function(request, response) {
    var query = request.query.keywords;
    var promise = new Promise(async (resolve, reject) => {
        var productList = await db.getProducts(query);
        resolve(productList);
    });

    promise.then(function(productList) {
        console.log(productList); // <--- productList is undefined here
        response.json({
            productList
        });     
    }).catch('Error retrieving data');
    // var productList = Promise.resolve(db.getProducts(query));
    // productList.then(products => response.json({products}));
});

MongoDB portion

StoreDB.prototype.getProducts = function(queryParams){
    return this.connected.then(function(db){
        // TODO: Implement functionality
        const collection = db.collection('products');
        var productList;

        if (!queryParams){
            console.log('no queryParams');
            collection.find({}).toArray(function(err, docs) {
            productList = convertToObject(docs);
            // console.log(productList); // <--- productList is defined here
            return productList;
            });
        ////////////

Some extra information:
– StoreDB is a functional class with only one property , connected, which is a promise that gets resolved once the connection to the database has been established
– The Express.js server calls getProducts which then resolves the promise
– Within the getProducts call, productList is defined, but when it returns, the value is undefined

With this being said, I thought that by putting the getProducts within in a promise, it would wait for the function call to be complete, then resolve it. However, that is not the case. Some insights would be greatly appreciated. Thanks.

SOLVED
Thanks to @Renan Le Caro

app.get('/products', function(request, response, next) {
  db
    .getProducts(request.query.keywords)
    .then(productList=>response.json({productList}))
    .catch(err=>next(err));
});

StoreDB.prototype.getProducts = function(queryParams){
  return this.connected.then(db => 
    new Promise((resolve, reject) => {
      db
        .collection('products')
        .find({})
        .toArray((err, docs) => err ?  reject(err) : resolve(docs))
    })
}

Leave an answer

Your email address will not be published. Required fields are marked *