JavaScript HTTP function return initial array values azure http function return json,azure function http return xml,

I've got this firebase cloud function that I've coded to count the number of registered users vs the anynymous users.

// Recalc totals user 
exports.recalcTotalUsers = functions.https.onRequest((req, res) => {
    var arr = {
        anonymous: 0,
        registered: 0
    };

    console.log('starting recalcTotalUsers');
    admin.database().ref("users").once("value").then((snapshot) => {
        snapshot.forEach(function (uid) {
            if (uid.child('/email').val() == ANONYMOUS_STRING) {
                arr.anonymous++;
            } else {
                arr.registered++;
            }
            console.log(`1. ${arr.anonymous}:${arr.registered}`)
        })
    });
    res.status(200).send(`ok registered: ${arr.registered}, anonymous: ${arr.anonymous}`)  
 });

When the function returns I get alway anonymous = 0 and registered = 0 event if they have different values... what I am doing wrong?

Answer:1

You have to move your response! Because at the moment, let's look at your code:

admin.database().ref("users").once("value").then((snapshot) => {
    snapshot.forEach(function (uid) {
        if (uid.child('/email').val() == ANONYMOUS_STRING) {
            arr.anonymous++;
        } else {
            arr.registered++;
        }
        console.log(`1. ${arr.anonymous}:${arr.registered}`)
    })
});
res.status(200).send(`ok registered: ${arr.registered}, anonymous: ${arr.anonymous}`)

As you might know, the database functions are all asynchronous, which means that they

  • take time to be executed
  • have promises and thus do not wait before the next line of code is executed

    res.status(200).send(ok registered: ${arr.registered}, anonymous: ${arr.anonymous})

That line is executed before you query through your "users" snapshot. You have to move it, and best practice would be another promises (.then(...)), but you can just do it like this:

admin.database().ref("users").once("value").then((snapshot) => {
    snapshot.forEach(function (uid) {
        if (uid.child('/email').val() == ANONYMOUS_STRING) {
            arr.anonymous++;
        } else {
            arr.registered++;
        }
        console.log(`1. ${arr.anonymous}:${arr.registered}`)
    })
    res.status(200).send(`ok registered: ${arr.registered}, anonymous: ${arr.anonymous}`)
});
Answer:2

I have two Input fields I want to name based on user input but this name should be lowercase and spaces between words change to "-". I am doing like this $(function() { $("#name").keyup(...

I have two Input fields I want to name based on user input but this name should be lowercase and spaces between words change to "-". I am doing like this $(function() { $("#name").keyup(...

  1. auto generate name in excel
  2. auto generate name
  3. auto generate file name
  4. auto generate company name
  5. auto generate business name
  6. auto generate rap name
  7. auto generate domain name
  8. auto generate team name
  9. auto generate instagram name
  10. auto generate computer name unattend.xml
  11. auto generate last name
  12. salesforce auto generate name
  13. auto generate file name word
  14. c# auto generate name
  15. auto generate excel file name
  16. java auto generate variable name
  17. mdt auto generate computer name
  18. php auto generate file name
  19. auto-generate unit-of-order name

I am trying to scrape the following Javascript frontend website to practise my Javascript scraping skills: https://www.oplaadpalen.nl/laadpaal/112618 I am trying to find two different elements by ...

I am trying to scrape the following Javascript frontend website to practise my Javascript scraping skills: https://www.oplaadpalen.nl/laadpaal/112618 I am trying to find two different elements by ...

  1. python selenium scraping tutorial
  2. python selenium scraping example
  3. python selenium scraping
  4. python selenium scraping quora
  5. python selenium scraping javascript
  6. python selenium scraping table
  7. python selenium scrape website
  8. python selenium web scraping
  9. python selenium web scraping tutorial
  10. python selenium web scraping example
  11. python selenium webdriver scraping
  12. python selenium firefox scraping
  13. python selenium data scraping
  14. python selenium screen scrape
  15. python selenium chrome web scraping
  16. python selenium phantomjs web scraping
  17. python 3 web scraping selenium

I have an array of objects that contain data like this [{ date: "01-01-2017 00:00:00", dataField1: "", dataField2: "" }, { date: "01-02-2017 00:00:00", dataField1: "", dataField2: "" }, { ...

I have an array of objects that contain data like this [{ date: "01-01-2017 00:00:00", dataField1: "", dataField2: "" }, { date: "01-02-2017 00:00:00", dataField1: "", dataField2: "" }, { ...

  1. split array objects
  2. split array objects javascript
  3. split objects array js
  4. split array into objects
  5. split array of objects php
  6. split array of objects lodash
  7. split array of json objects
  8. split array of json objects python
  9. javascript split array of objects by property
  10. logstash split array of objects
  11. split json array into objects
  12. javascript split array into objects
  13. powershell split array of objects
  14. typescript split array of objects by property
  15. java split array of objects
  16. typescript split array of objects

I am using OpenShift and Node.js I am trying to get the average - rating for each result but I cant get the response to work even though the console reports correctly. I get 3.9454323 into the ...

I am using OpenShift and Node.js I am trying to get the average - rating for each result but I cant get the response to work even though the console reports correctly. I get 3.9454323 into the ...

  1. openshift node express