0

I'm new to nodejs and there's a problem I can not solve. I use nodejs and firebase database. In the database I have a collection that I have on "engineers", and I want move the data from the database to the array.

This is the database I have: I want to turn preferences into an array:

enter image description here

I want to get this array:

  const engineers = [
    // frontend engineers
    { html: 5, angular: 5, react: 3, css: 3 },
    { html: 4, react: 5, css: 4 },
    { html: 4, react: 5, vue: 4, css: 5 },
    { html: 3, angular: 3, react: 4, vue: 2, css: 3 },

    // backend engineers
    { nodejs: 5, python: 3, mongo: 5, mysql: 4, redis: 3 },
    { java: 5, php: 4, ruby: 5, mongo: 3, mysql: 5 },
    { python: 5, php: 4, ruby: 3, mongo: 5, mysql: 4, oracle: 4 },
    { java: 5, csharp: 3, oracle: 5, mysql: 5, mongo: 4 },

    // mobile engineers
    { objc: 3, swift: 5, xcode: 5, crashlytics: 3, firebase: 5, reactnative: 4 },
    { java: 4, swift: 5, androidstudio: 4 },
    { objc: 5, java: 4, swift: 3, androidstudio: 4, xcode: 4, firebase: 4 },
    { objc: 3, java: 5, swift: 3, xcode: 4, apteligent: 4 },

    // devops
    { docker: 5, kubernetes: 4, aws: 4, ansible: 3, linux: 4 },
    { docker: 4, marathon: 4, aws: 4, jenkins: 5 },
    { docker: 3, marathon: 4, heroku: 4, bamboo: 4, jenkins: 4, nagios: 3 },
    { marathon: 4, heroku: 4, bamboo: 4, jenkins: 4, linux: 3, puppet: 4, nagios: 5 }
  ];

This is the code I wrote down so far, but it does not work for me.

//enginer.js file

const admin = require('firebase-admin');
admin.initializeApp();
const db = admin.firestore();

exports.getPreferancesArray = (req,res) => {
  let engineers = {};
  db.collection(`/preferences`).get().then((doc) => {
    if (doc.exists) {
      engineers = doc.data();
      console.log(engineers);
    }
    res.json(engineers);
  });
};

//index.js
const {
  getPreferancesArray
} = require('./enginer');

const cors = require('cors');
app.use(cors());

app.get('/kmeansArray', FBAuth, getPreferancesArray);

exports.api = functions.region('europe-west1').https.onRequest(app);

The problem is: I can not turn content from the database into an array, I do not understand what the problem I have been delaying it for several hours. I want the enginer variable to have the array

1
  • If you are calling get on a collection then it should return a query snapshot. That snapshot contains the documents you are looking for. Try to map the documents to an array. Commented Dec 19, 2020 at 13:01

1 Answer 1

1

The following should do the trick:

let engineers = [];

db.collection("preferences").get().then(querySnapshot => {
    console.log(`Found ${querySnapshot.size} documents.`);
    querySnapshot.forEach(doc => {
        // doc.data() is never undefined for query doc snapshots
        const engineerDetails = doc.data();
        engineers.push(engineerDetails);
    });

    // Here the engineers array is fully populated
    console.log(engineers.length);
    res.json(engineers);

});

We first declare and empty array, then we loop over the documents in the preferences collection and, for each document, we push (i.e. add) the object returned by the data() method to the array. At the end we have an Array containing all the engineers data.


Note that this will return the field names with the underscore (i.e. { _html: 5, _angular: 5, _react: 3, _css: 3 }) because we don't do any transformation on the object returned by data(). If you want to remover the _, it's up to you to manipulate the object returned by data().

Sign up to request clarification or add additional context in comments.

8 Comments

Thank you for the help, the array is empty, can you tell me why it does not work for me?
I have made a mistake... forgot the loop! Updated!
Thanks a lot for your help, I'm trying to run the code, it seems everything is working, but I can not see what's inside the array, it shows me that the array is empty
I’ve added a console.log in the answer to log the size of the array. What do you see in you log?
After I do res.json(engineers); I do console.log (engineers) and I get blank arrays of array. []. Is the array really empty? Or is it full and can I use it for calculations?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.