2

First, i'd like to say that a lot of my code is probably horrible so my apologies. My .ts and .js experience is not at a point where I feel super comfortable (hence why I can't solve this). So i'm trying to get document content from one document to another. Currently doing it via snapshots though i'm sure there's faster and better ways. My code is below.

The error happens when I try to set the second collection (redacted2)'s document to the snapshot data. As you can tell from the console logs, I know that the snapshot.data() has the valuable information I need formatted exactly the way I want it to be. I'm assuming this is similar to having a guard statement in swift where I can check the value for null and assign it to a given type (but I could be wrong here).

                db.collection("Redacted").doc(context.params.sensors).get()
                .then(function(querySnapshot){
                    console.log("Query Snapshot is is isssss: ", querySnapshot)
                    console.log("Query Snapshot querySnapshot.data: ", querySnapshot.data)
                    console.log("Query Snapshot querySnapshot.data(): ", querySnapshot.data())
                    console.log("Query Snapshot querySnapshot.ref: ", querySnapshot.ref)
                    console.log("Query Snapshot querySnapshot.get: ", querySnapshot.get)

                    return querySnapshot
                }).then(function(querySnapshotData) {
                    console.log("ayoooooo blooooddd", querySnapshotData)
                    db.collection("Redacted2").doc(context.params.sensors).set(querySnapshotData.data())
                        .then((alertResponse1: any) => {
                            console.log("Successfully updated firestore: ", alertResponse1)
                        })
                        .catch ((alertError1: any) => {
                            console.log("Successfully updated firestore but with error: ", alertError1)
                        })
                })
                .catch(function(error) {
                    console.log("query snapshot error: ", error)
                });

1 Answer 1

10

As you can see from the API documentation for DocumentSnapshot.data(), data() is declared to return either DocumentData or undefined. It could be undefined if the document did not exist. TypeScript will not let you call methods on something that could possibly be undefined, as that will generate an error at runtime. You will have to assure TypeScript that the result of that is actually a DocumentData and never undefined.

const data = snapshot.data()
if (data) {
    // data will never be undefined here, because we just checked that.
    const foo = data.foo
}
Sign up to request clarification or add additional context in comments.

1 Comment

You can also cast the value const docSnap = await getDoc(this.getRef(key)); return docSnap.exists ? docSnap.data() as T : null

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.