Я следовал точно такой же процедуре, показанной Дагом Стивенсоном. Все еще не знаю, почему я получаю эту ошибку. Я совершенно новичок в облачных функциях. Я получаю эту ошибку:
onMessageCreate
TypeError: Cannot read property 'replace' of undefined
at addPizzazz (/srv/lib/index.js:16:17)
at exports.onMessageCreate.functions.database.ref.onCreate (/srv/lib/index.js:12:18)
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)
Мой файл index.ts
import * as functions from 'firebase-functions'
export const onMessageCreate = functions.database
.ref('/rooms/{roomId}/messages/{messageId}')
.onCreate((snapshot, context) => {
console.log(`triggered onCreate`)
const roomId = context.params.roomId
const messageId = context.params.messageId
console.log(`New Message ${messageId} in room ${roomId}`)
const messageData = snapshot.val()
const text = addPizzazz(messageData.text)
return snapshot.ref.update({ text: text })
})
function addPizzazz ( text: string):string{
return text.replace(/\bpizza\b/g,'?')
}
И моя база данных выглядит следующим образом.
И для создания своей базы данных, как и Даг, я использовал этот код в своем приложении.
String s1 = myRef.push().getKey();
myRef.child("rooms").child("pizzachat").child("messages").child(s1).child("text").setValue("What the hack is that?!");
myRef.child("rooms").child("pizzachat").child("messages").child(s1).child("name").setValue("Fear");
String s2 = myRef.push().getKey();
myRef.child("rooms").child("pizzachat").child("messages").child(s2).child("text").setValue("Who puts broccoli in pizza?!");
myRef.child("rooms").child("pizzachat").child("messages").child(s2).child("name").setValue("Joy");
String s3 = myRef.push().getKey();
myRef.child("rooms").child("pizzachat").child("messages").child(s3).child("text").setValue("That's it. I'm done.");
myRef.child("rooms").child("pizzachat").child("messages").child(s3).child("name").setValue("Disgust");
String s4 = myRef.push().getKey();
myRef.child("rooms").child("pizzachat").child("messages").child(s4).child("text").setValue("Congratulations pizza");
myRef.child("rooms").child("pizzachat").child("messages").child(s4).child("name").setValue("Anger");
Я вообще не знаю Android и Firebase. Я пытался найти помощь, но я не получил. Я начал изучать только те модули, которые помогут мне реализовать мою идею.
Как я могу это исправить?