Firebase Cloud Functions Object possibly 'undefined'

前端 未结 2 1544
迷失自我
迷失自我 2021-01-21 07:07

I have the following code in typescript and i get this error on the line: change.after.data();, Object is posibbly \'undefined\':

import * as functi         


        
2条回答
  •  说谎
    说谎 (楼主)
    2021-01-21 07:19

    If you look at the types for the onUpdate handler, the argument for change has 2 optional properties, after and before:

    class Change {
        before?: T;
        after?: T;
        constructor(before?: T, after?: T);
    }
    

    Because you want to access after, you'll need to wrap it in a conditional, something like this following:

    functions.firestore.document("Settings/ShiftsEditMode").onUpdate(change=> {
        if (change.after) {
            const after = change.after.data();
            ...
        }
    }
    

提交回复
热议问题