after not finding anything online, I decided to write here. I need to get all the document in the /user/uid
collection in Node JS. When I use the Firebase code it replies like this:
Promise {<pending>}
Please help me!
PS: I don't want to use Firebase Admin SDK and await.
"Full" code
var express = require('express');
const { initializeApp } = require("firebase/app");
const { getDatabase, ref, get, child, set, onValue} = require("firebase/database");
const { getFirestore, collection, getDoc, doc} = require("firebase/firestore");
const firebaseApp = initializeApp(firebaseConfig);
// Get a reference to the database service
var app = express();
const PORT = process.env.PORT || 5050
app.get('/', (req, res) => {
res.send('This is my demo project')
});
const db = getDatabase();
const firestore = getFirestore();
const document = getDoc(doc(collection(firestore, 'users'), "BW5kylV6rtZXtYibpKGc2m1asRm1"));
console.log(document);
//Response: "Promise {<pending>}""
CodePudding user response:
Firebase method calls are asynchronous. So for both the Admin SDK or the JS SDK you have to deal with the asynchronicity, either by using then
or by using async/await
.
If you receive Promise {<pending>}
it is because the asynchronous method getDoc
returns a Promise and this promise is not fulfilled, but is still pending.
So you need to dive into the documentation on how to use Promises... or the async/await
keywords.
In your case, the following will do the trick:
getDoc(doc(collection(firestore, 'users'), "BW5kylV6rtZXtYibpKGc2m1asRm1"))
.then(snap => {
console.log(snap.data())
})
However, if you write this code for running on a Node server you own, I would recommend to use the Admin SDK.