Cloud Firestore: TypeError: Невозможно прочитать свойство 'ref' неопределенного
Я использую функции Cloud для обновления номера комментариев в родительской коллекции Cloud Firestore, поэтому, когда в комментарии добавлены функции Cloud, можно автоматически обновлять номер комментария.
exports.updateCommentNumbers = functions.firestore
.document('postlist/{postlistId}/comments/{commentsID}')
.onCreate(event =>
{
const collectionRef = event.after.ref.parent;
const countRef = collectionRef.parent.child('comment_number');
//const previousValue = event.data.previous.data();
let increment;
if (event.after.exists() )
{
increment = 1;
}
else
{
return null;
}
return countRef.transaction((current) =>
{
return (current || 0) + increment;
}).then(() =>
{
return console.log('Comments numbers updated.');
});
});
Я получил ошибку, которую я не понимаю. Не могли бы вы рассказать мне, что случилось?
TypeError: Невозможно прочитать свойство ref в undefined at export.updateCommentNumbers.functions.firestore.document.onCreate.event(/user_code/index.js:46:35) в Object. (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:112:27) в следующем (native) в /user_code/node_modules/firebase-functions/lib/cloud-functions.js:28:71 в __awaiter (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:24:12) в cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:82:36) в /var/tmp/worker/worker.js: 716: 24 на process._tickDomainCallback (internal/process/next_tick.js: 135: 7)
Как показано в этом руководстве по обновлению, подпись Cloud Function для Firebase имеет изменения при переключении на v1.0.
Если вы использовали это до версии 1.0:
exports.dbWrite = functions.firestore.document('/path').onWrite((event) => {
const beforeData = event.data.previous.data(); // data before the write
const afterData = event.data.data(); // data after the write
});
Теперь это:
exports.dbWrite = functions.firestore.document('/path').onWrite((change, context) => {
const beforeData = change.before.data(); // data before the write
const afterData = change.after.data(); // data after the write
});
Вместо того, чтобы переписывать код для вас, я рекомендую вам обновить его на основе этой документации или проверить, где вы получили код, чтобы узнать, есть ли обновленная версия.