Cloud Functions-TypeScript- “Object is possibly 'undefined'.ts(2532)”

后端 未结 2 1476
难免孤独
难免孤独 2021-01-06 23:43

This is my code:

export const newPost = functions.firestore
.document(\'post/{postId}/\')
.onCreate((snap, context) => {

    const postData = snap.data()         


        
相关标签:
2条回答
  • 2021-01-07 00:19

    A bit hacky, but a quick fix const postData: any = snap.data()

    0 讨论(0)
  • 2021-01-07 00:26

    Your TypeScript configuration almost certainly has strict type checking enabled, which will give you this warning when you try to access properties of something that could be null or undefined. Check your tsconfig.json and look for "strict": true" in the compiler options. The TypeScript bindings for the DataSnapshot.data() API says that the return value of data() could anything (including null or undefined), and TypeScript is forcing you to deal with this fact correctly at compile time so that your code doesn't crash at runtime.

    The sample code you're looking at is plain JavaScript, which does not have any type checking. It is assuming that the snapshot will not be null or undefined. If you found this to be confusing or problematic, please use the "send feedback" link at the top of the page of documentation to explain what was confusing to you.

    0 讨论(0)
提交回复
热议问题