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
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();
...
}
}