【发布时间】:2019-06-01 14:26:06
【问题描述】:
我有一个有趣的问题...
我将official documentation 用于firestore-events
然后我从那里得到这个代码,以便测试和玩耍。
exports.countNameChanges = functions.firestore
.document('users/{userId}')
.onUpdate((change, context) => {
// Retrieve the current and previous value
const data = change.after.data();
const previousData = change.before.data();
// We'll only update if the name has changed.
// This is crucial to prevent infinite loops.
if (data.name == previousData.name) return null;
// Retrieve the current count of name changes
let count = data.name_change_count;
if (!count) {
count = 0;
}
// Then return a promise of a set operation to update the count
return change.after.ref.set({
name_change_count: count + 1
}, { merge: true });
});
我放了那些进口的(所以也许我错过了一个)
import * as functions from 'firebase-functions';
import * as Storage from '@google-cloud/storage';
const gcs = new Storage.Storage();
我认为一切看起来都很好,但我收到以下错误消息:
src/index.ts:33:13 - error TS2532: Object is possibly 'undefined'.
33 if (data.name == previousData.name) return null;
~~~~
src/index.ts:33:26 - error TS2532: Object is possibly 'undefined'.
33 if (data.name == previousData.name) return null;
~~~~~~~~~~~~
src/index.ts:36:21 - error TS2532: Object is possibly 'undefined'.
36 let count = data.name_change_count;
~~~~
发生了什么事?我在这里缺少什么?有谁知道我做错了什么并且可以帮助我?我在想,文档中的一切都应该很好并且可行。
【问题讨论】:
标签: typescript firebase google-cloud-firestore google-cloud-functions