Я недавно начал играть с облачными функциями и получаю эту ошибку:
> TypeError: Cannot read property 'data' of undefined
> at exports.createPost.functions.firestore.document.onCreate (/srv/index.js:15:37)
> at cloudFunction (/srv/node_modules/firebase-functions/lib/cloud-functions.js:131:23)
> at /worker/worker.js:825:24
> at <anonymous>
> at process._tickDomainCallback (internal/process/next_tick.js:229:7)
, и это мой код
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const algoliasearch = require('algoliasearch');
const ALGOLIA_APP_ID = "";
const ALGOLIA_ADMIN_KEY = "";
const ALGOLIA_INDEX_NAME = "Posts";
admin.initializeApp(functions.config().firebase);
//const firestore = admin.firestore;
exports.createPost = functions.firestore
.document('User/{UserId}/Posts/{PostsID}')
.onCreate( async (snap, context) => {
const newValue = snap.after.data();
newValue.objectID = snap.after.id;
var client = algoliasearch(ALGOLIA_APP_ID, ALGOLIA_ADMIN_KEY);
var index = client.initIndex(ALGOLIA_INDEX_NAME);
index.saveObject(newValue);
});
Функция onCreate срабатывает в нужное время, и проблема только ошибка. Я сделал свое исследование и не мог понять это. Я надеюсь, что смогу получить некоторую помощь.
Заранее спасибо:).