and-nd-firebase icon indicating copy to clipboard operation
and-nd-firebase copied to clipboard

Error in Cloud Functions database Cannot read property 'val' of undefined

Open Cherudek opened this issue 6 years ago • 8 comments

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

Cherudek avatar May 15 '18 23:05 Cherudek

am facing same problem us well..... any solution to that guys

Akwaboah avatar Aug 11 '18 02:08 Akwaboah

can someone teach me how to push codes in hear as well ?

Akwaboah avatar Aug 11 '18 02:08 Akwaboah

@Akwaboah Just refer to Cherudek's code on top, and it will work as per expected.

jerrychong25 avatar Nov 05 '18 15:11 jerrychong25

@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 avatar Nov 05 '18 21:11 gxlinda

@gxlinda, I am using Firebase version 6.0.1 as well. But i don't encountered this error.

jerrychong25 avatar Nov 06 '18 01:11 jerrychong25

@jerrychong25 I used this code, and it finally works: https://discussions.udacity.com/t/firebase-lesson-3-deploy-and-test-error/656031/3

gxlinda avatar Nov 06 '18 09:11 gxlinda

@gxlinda, glad to hear that. But I not sure why it doesn't work for your side.

jerrychong25 avatar Nov 06 '18 14:11 jerrychong25

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;
}

timtsj avatar Mar 03 '19 16:03 timtsj