0
votes

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.; Desired Outcome

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)

Code

The updateCarrier function in the image works. I'm having trouble with pushCurrentHolder function.

Highly appreciate if anyone can help! Thanks in advance.

3

3 Answers

2
votes

You're mixing up Realtime Database and Firestore API. They are not the same database and have completely different APIs.

There is no push() method in Firestore. There is also no child() method. As such, this code is doesn't make any sense:

firebase.firestore()
    .collection('packages')
    .doc(`${parcelId}`)
    .child("packageTransactions")
    .push()
    .set({"userId": user.uid, "transactionTime": moment().format("YYYY-MM-DD HH:mm:ss")})

If you want to add a document to the packages collection with a random ID, do this instead:

firebase.firestore()
    .collection('packages')
    .doc(parcelId)
    .add({"userId": user.uid, "transactionTime": moment().format("YYYY-MM-DD HH:mm:ss")})

I'm sure this will not do exactly what you want, but you can see how the API works.

I strongly suggest reviewing the Firestore documentation for writing data in order to understand the APIs better.

1
votes

Both firestore & realtime APIs aren't the same. You are mixing up the methods of both the APIs. The push() & child() methods are of realtime API not firestore. To achieve what you want, do:

const db = firebase.firestore()

let transactRef = db.collection("packages")
                    .doc(parcelId)
                    .collection("packageTransactions")

transactRef.add({
  userId: user.uid,
  transactionTime: moment().format("YYYY-MM-DD HH:mm:ss")
}) 
1
votes

You are mixing between firebase realtime database and firestore, there is no child() method in firestore:

var db= firebase.firestore();
var ref = db.collection("packages").doc(`${parcelId}`);
var transactRef = ref.collection("packageTransactions");
transactRef.add({
  "userId": user.uid, 
  "transactionTime": moment().format("YYYY-MM-DD HH:mm:ss")
})

Check the docs:

https://firebase.google.com/docs/firestore/manage-data/add-data#web