I'm quite new to working with firebase. I am looking at a way to push data to a list in firebase firestore.
My desired outcome is as in the image below. When there is a new transaction, I need to push that to "packageTransactions" with a new ID. I read in Documentation that the ID can be auto generated and it is derived from timestamp. This would be perfect for me since then ordering is easy.;
I tried achieving this in two ways;
Method 1:
return firebase.firestore()
.collection('packages')
.doc(`${parcelId}`)
.child("packageTransactions")
.push()
.set({"userId": user.uid, "transactionTime": moment().format("YYYY-MM-DD HH:mm:ss")})
This generates error:
[Unhandled promise rejection: TypeError: firebase.firestore().collection('packages').doc("" + parcelId).child is not a function. (In 'firebase.firestore().collection('packages').doc("" + parcelId).child("packageTransactions")', 'firebase.firestore().collection('packages').doc("" + parcelId).child' is undefined)]
Method 2:
var db= firebase.firestore();
var ref = db.ref(`packages/${parcelId}/`);
var transactRef = ref.child("packageTransactions");
transactRef.push().set({
"userId": user.uid,
"transactionTime": moment().format("YYYY-MM-DD HH:mm:ss")
})
This generates error:*
[Unhandled promise rejection: TypeError: db.ref is not a function. (In 'db.ref("packages/" + parcelId + "/")', 'db.ref' is undefined)
The updateCarrier function in the image works. I'm having trouble with pushCurrentHolder function.
Highly appreciate if anyone can help! Thanks in advance.