and-nd-firebase
and-nd-firebase copied to clipboard
Error in Cloud Functions database Cannot read property 'val' of undefined
Firebase SDK for Cloud Functions Migration Guide: Beta to version 1.0
In the code provided we need to remove the variable (event) and replace it with (change,context): this is the snippet of my working code:
exports.emojify =
functions.database.ref('/messages/{pushId}/text')
.onWrite((change,context) => {
console.log("emojifying!");
// Get the value from the 'text' key of the message
const originalText = change.after.val();
const emojifiedText = emojifyText(originalText);
// Return a JavaScript Promise to update the database node
return change.after.ref.set(emojifiedText);
});
Reference from this StackOverflow post: https://stackoverflow.com/questions/49746905/firebase-typeerror-cannot-read-property-val-of-undefined?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa
am facing same problem us well..... any solution to that guys
can someone teach me how to push codes in hear as well ?
@Akwaboah Just refer to Cherudek's code on top, and it will work as per expected.
@jerrychong25 , @Cherudek I tried this code, but it does not work for me (firebase version 6.0.1 from command line). I get the error 'ReferenceError: change is not defined'
@gxlinda, I am using Firebase version 6.0.1 as well. But i don't encountered this error.
@jerrychong25 I used this code, and it finally works: https://discussions.udacity.com/t/firebase-lesson-3-deploy-and-test-error/656031/3
@gxlinda, glad to hear that. But I not sure why it doesn't work for your side.
work for me:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
// replaces keywords with emoji in the "text" key of messages
// pushed to /messages
exports.emojify =
functions.database.ref('/messages/{pushId}/text')
.onWrite((change,context) => {
console.log("emojifying!");
// Get the value from the 'text' key of the message
const originalText = change.after.val();
const emojifiedText = emojifyText(originalText);
// Return a JavaScript Promise to update the database node
return admin.database().ref("/messages/" + context.params.pushId + "/").update({ text: emojifiedText });
});
// Returns text with keywords replaced by emoji
// Replacing with the regular expression /.../ig does a case-insensitive
// search (i flag) for all occurrences (g flag) in the string
function emojifyText(text) {
var emojifiedText = text;
emojifiedText = emojifiedText.replace(/\blol\b/ig, "😂");
emojifiedText = emojifiedText.replace(/\bcat\b/ig, "😸");
return emojifiedText;
}