Arjun Ram
Arjun Ram

Reputation: 369

How to Retrieve individual JSON values from Firebase Database?

Let's say I have a firebase node that is dbref = firebase.ref('/Transfer_Request/{pushID]/').

And the client writes two values; from_ID and to_ID to dbref. How do I get the individual values of the from_ID and to_ID from Firebase Cloud functions?

My database

My code:

exports.TransferTicket = functions.database.ref('/Transfer_Request/{pushID}').onWrite((event) => {

  const original = event.data.val();
  const from_ID = original.from_ID;
  const to_email_ID = original.to_ID;

  //search for to_email ID
  return admin.database().set("A transfer request was just made");

});

I'm getting two errors:

1)

TypeError: admin.database(...).set is not a function at exports.TransferTicket.functions.database.ref.onWrite (/user_code/index.js:41:25) at Object. (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:59:27) at next (native) at /user_code/node_modules/firebase-functions/lib/cloud-functions.js:28:71 at __awaiter (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:24:12) at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:53:36) at /var/tmp/worker/worker.js:716:24 at process._tickDomainCallback (internal/process/next_tick.js:135:7)

2)

TypeError: Cannot read property 'from' of null at exports.TransferTicket.functions.database.ref.onWrite (/user_code/index.js:35:25) at Object. (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:59:27) at next (native) at /user_code/node_modules/firebase-functions/lib/cloud-functions.js:28:71 at __awaiter (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:24:12) at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:53:36) at /var/tmp/worker/worker.js:716:24 at process._tickDomainCallback (internal/process/next_tick.js:135:7)

Upvotes: 0

Views: 734

Answers (1)

Renaud Tarnec
Renaud Tarnec

Reputation: 83181

The first problem comes from the fact that when doing the following you miss a Firebase Reference.

return admin.database().set("A transfer request was just made");

You should do:

admin.database().ref('...the path where you want to write...').set("A transfer request was just made");

For more details, see the doc for Reference and Database .

The second problem comes from the fact that since the new release of the version 1.0.0 of the Firebase SDK for Cloud Functions, the syntax has changed. See this doc item.

You should modify your code as follows:

exports.TransferTicket = functions.database.ref('/Transfer_Request/{pushID}').onWrite((change, context) => {

    const original = change.after.val();

    const from_ID = original.from_ID;
    console.log(from_ID);
    const to_email_ID = original.to_ID;
    console.log(to_email_ID);

    return admin.database().ref('...path...').set("A transfer request was just made")
    .catch(error => {
        console.log(error);
        //...
    });

});

Upvotes: 1

Related Questions