0

On the firebase functions, I have next code:

app.post('/licence', (req, res) => {
    let { email, machine_id, product_id } = req.body
    let arr_product_ids = product_id.split(",").map(function (val) { return {product_id: val}; }); 
    let res_to_print = '';
    return new Promise((resolve, reject) => {
        arr_product_ids.forEach(function(n){
            res_to_print = asyncGetLicences(n.product_id, email, machine_id)
            console.log('res_to_print')
            console.log(res_to_print)
        });

    }).then((state) => {
        console.log(state)
    })
    .catch((error) => {
        console.log(error)
    });

I need to call query two times to firebase query! So I call it in foreach loop.

Here is the function that needs to be called twice:

function asyncGetLicences(product_id, email, machine_id) {
    licences_to_print = []

    db.collection('licences', 'desc').where('email', '==', email).where('product_id', '==', product_id).get()
    .then(data => {
        let licences = []
        data.forEach(doc => {
            console.log(doc.data().email);
            licences.push({
                id: doc.id,
                email: doc.data().email,
                product: doc.data().product,
                createdAt: doc.data().createdAt
            });
        });
        if(typeof this.licences !== 'undefined' && this.licences.length > 0){
            let string = email+machine_id+product_id+'55';
            let api = md5(string);
            let uppercase = api.toUpperCase()+'-';
            licences_to_print.push(uppercase);

            return licences_to_print
            //return res.send('"'+uppercase+'"');//res.json(this.licences);
        } else {
            return licences_to_print
            //return res.status(200).send('nothing to find');
        }

    })
}

I'm struggling with this simple promise...I had this in PHP and it was very easy, but node.js and firebase I got stuck!

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
user1405338
  • 179
  • 1
  • 5
  • 14

1 Answers1

1

Add all the promises in an array and insert into Promise.all() and then return this in the main function. This will collectively get the return from each promises asynchronously and return a single collective response.

app.post('/licence', (req, res) => {
let { email, machine_id, product_id } = req.body
let arr_product_ids = product_id.split(",").map(function (val) { return {product_id: val}; }); 
let res_to_print = '';

const promises = []  // Empty array
arr_product_ids.forEach(function(n){
    promises.push(asyncGetLicences(n.product_id, email, machine_id));
});

return Promise.all(promises).then(res_to_print => {
    console.log('res_to_print')
    console.log(res_to_print)
}).catch((error) => {
    console.log(error)
});

The second function:

function asyncGetLicences(product_id, email, machine_id) {
licences_to_print = []

return new Promise((resolve, reject) => {
db.collection('licences', 'desc').where('email', '==', email).where('product_id', '==', product_id).get()
.then(data => {
    let licences = []
    data.forEach(doc => {
        console.log(doc.data().email);
        licences.push({
            id: doc.id,
            email: doc.data().email,
            product: doc.data().product,
            createdAt: doc.data().createdAt
        });
    });
    if(typeof this.licences !== 'undefined' && this.licences.length > 0){
        let string = email+machine_id+product_id+'55';
        let api = md5(string);
        let uppercase = api.toUpperCase()+'-';
        licences_to_print.push(uppercase);

        resolve(licences_to_print)
        //return res.send('"'+uppercase+'"');//res.json(this.licences);
    } else {
        resolve(licences_to_print)
        //return res.status(200).send('nothing to find');
    }
}))
Mohammed Mukhtar
  • 1,731
  • 20
  • 18