Kingsley CA
Kingsley CA

Reputation: 11634

Node.js - Not running DB promises before returning response

I have this request handler on my node server. It has three MongoDB queries, and I want all the results to be returned, before the response is sent.

api.get('/getStats/:productID', (req,res)=>{
  let data = {};
  let dailySales = [];
  let avgProduct = "";
  let customers = [];

  Sales.find({productID: productID}).then(
    sales => {
      dailySales = sales;
    }
  );

  Products.find({}).then(
    products => {
      // Calculate Avg product here
      avgProduct = result;
    }
  );

  Customers.find({}).then(
    customers => {
      customers = customers;
    }
  );

  data = {
    dailySales,
    avgProduct,
    customers
  };

  res.json(data);
});

But running this returns

data: {
  dailySales: [],
  avgProduct: "",
  customers: []
}

i.e. The Mongo response is returning before the data is run. Please how to I fix. Thank You

Upvotes: 0

Views: 49

Answers (3)

Kunal Mukherjee
Kunal Mukherjee

Reputation: 5853

Try using the in-built util.promisify function along with async-await to get data correctly!

const promisify = require('utils').promisify;

const salesFindOnePromise = promisify(Sales.find);
const productsFindAllPromise = promisify(Products.find);
const customersFindAllPromise = promisify(Customers.find);

findDailySalesByIdAsync = async (productID) => {

    try {
        return await salesFindOnePromise({ productID: productID });
    } catch(err) {
        throw new Error('Could not fetch the appropriate sales with productID');
    }

}

findProductsAsync = async () => {

    try {
        return await productsFindAllPromise({});
    } catch (err) {
        throw new Error('Could not fetch sales!');
    }
}

findCustomersAsync = async () => {

    try {
        return await customersFindAllPromise({});
    } catch (err) {
        throw new Error('Could not fetch customers!');
    }
}


api.get('/getStats/:productID', async (req,res)=>{

try {
  const dailySales = await findDailySalesByIdAsync(productID);
  const avgProduct = await findProductsAsync();
  const customers = await findCustomersAsync();

    const data = {
        dailySales,
        avgProduct,
        customers
    };

    return res.status(200).send(data);

  } catch(err) {
    console.err(`Failed because: {err}`);
    throw new Error('Could not fetch data because of some error!');
  }

});

Upvotes: 0

Karim
Karim

Reputation: 8652

wait for all the promises to resolve before sending the actual response

const sales = Sales.find({productID: productID});
const allProducts = Products.find({});
const allCustomers = Customers.find({});

Promise.all([sales, allProducts, allCustomers])
 .then(data => res.json(data));  

Upvotes: 5

ravi
ravi

Reputation: 1145

you can try using the Promise.all where you can pass the MongoDB queries as parameter to it ,the promise will be resolved when all the queries return the result in the array

Upvotes: 1

Related Questions