Reputation: 2563
Attached is the firebase db structure screenshot.
I want to get the data using node script. below is my code.
var db = admin.database();
var ref = db.ref("test/name");
console.log(ref)
ref.on("value", function(snapshot) {
console.log("==========");
console.log(snapshot.val());
}, function (errorObject) {
console.log("The read failed: " + errorObject.code);
});
console.log(snapshot.val());
always print null
Below is the result of console.log(ref)
path: Path { pieces_: [ 'test', 'name' ], pieceNum_: 0 },
queryParams_: QueryParams {
limitSet_: false,
startSet_: false,
startNameSet_: false,
endSet_: false,
endNameSet_: false,
limit_: 0,
viewFrom_: '',
indexStartValue_: null,
indexStartName_: '',
indexEndValue_: null,
indexEndName_: '',
index_: PriorityIndex {}
},
What is the issue in the code? referring this document: https://firebase.google.com/docs/reference/js/firebase.database.DataSnapshot
Upvotes: 0
Views: 234
Reputation: 80914
You are using firestore but your code is for the realtime database, you need to change the code to the following:
var db = admin.firestore();
var ref = db.doc("test/name");
console.log(ref)
let getDoc = ref.get()
.then(doc => {
if (!doc.exists) {
console.log('No such document!');
} else {
console.log('Document data:', doc.data());
}
})
.catch(err => {
console.log('Error getting document', err);
});
https://firebase.google.com/docs/firestore/query-data/get-data
Upvotes: 1