question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:5
  • Comments:8

github_iconTop GitHub Comments

1reaction
timtsjcommented, Mar 3, 2019

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;
}
1reaction
gxlindacommented, Nov 6, 2018
Read more comments on GitHub >

github_iconTop Results From Across the Web

Cloud Function error "Cannot read property 'data' of undefined"
I recently started playing with cloud functions and I am getting this error: > TypeError: Cannot read property 'data' of undefined > at ......
Read more >
Troubleshooting Cloud Functions - Google Cloud
Cloud Functions deployment can fail if the entry point to your code, that is, the exported function name, is not specified correctly. The...
Read more >
Cloud Function returned "Cannot read property 'attributes' of ...
This error usually occurs when you try to read an attribute of array, not of object(which u probably wanted). Recheck the value types....
Read more >
Call functions from your app | Cloud Functions for Firebase
Sending back the result; Handle errors; Deploy the callable function ... the default us-central1 , you must set the appropriate value at initialization....
Read more >
Cannot read properties of undefined (reading 'firestore')" mean?
I have a cloud function that is being executed successfully but it only returns a null value as a response to my app....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found