最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Error "snapshot.val" is not a function in Google Cloud Functions - Stack Overflow

programmeradmin1浏览0评论

When creating a new node, I want to create and push that same data into a different node.

The "ins" node is the node I am pushing the new data into:

root: { 
  doors: {
    111111111111: {
       MACaddress: "111111111111",
       inRoom: "-LBMH_8KHf_N9CvLqhzU",
       ins: {
          // I am creating several "key: pair"s here, something like:
          1525104151100: true,
          1525104151183: true,
       }
    }
  },
  rooms: {
    -LBMH_8KHf_N9CvLqhzU: {
      ins: {
        // I want it to clone the same data here:
        1525104151100: true,
        1525104151183: true,
      }
    }
  }

My function code is the following but it's not working at all. I can't even start the function when I use the onCreate trigger (which is all I need). Any ideas on how to make this work?

exports.updateRoom = functions.database.ref('doors/{MACaddress}/ins')
.onCreate((snapshot, context) => {
    const timestamp = snapshot.val();
    const roomPushKey = functions.database.ref('doors/{MACaddress}/inRoom');
    console.log(roomPushKey);  
    return snapshot.ref.parent.parent.child('rooms').child(roomPushKey).child('ins').set(timestamp);
});  

Note: I have fiddled with the code and I got it to run by changing the trigger to onWrite but like this I get an error message: "snapshot.val" is not a function...

exports.updateRoom = functions.database.ref('doors/{MACaddress}/ins').onWrite((snapshot, context) => {     
    const timestamp = snapshot.val();
    const roomPushKey = functions.database.ref('doors/{MACaddress}/inRoom');
    console.log(roomPushKey);
    return snapshot.ref.parent.parent.child('rooms').child(roomPushKey).child('ins').set(timestamp);
});  

When creating a new node, I want to create and push that same data into a different node.

The "ins" node is the node I am pushing the new data into:

root: { 
  doors: {
    111111111111: {
       MACaddress: "111111111111",
       inRoom: "-LBMH_8KHf_N9CvLqhzU",
       ins: {
          // I am creating several "key: pair"s here, something like:
          1525104151100: true,
          1525104151183: true,
       }
    }
  },
  rooms: {
    -LBMH_8KHf_N9CvLqhzU: {
      ins: {
        // I want it to clone the same data here:
        1525104151100: true,
        1525104151183: true,
      }
    }
  }

My function code is the following but it's not working at all. I can't even start the function when I use the onCreate trigger (which is all I need). Any ideas on how to make this work?

exports.updateRoom = functions.database.ref('doors/{MACaddress}/ins')
.onCreate((snapshot, context) => {
    const timestamp = snapshot.val();
    const roomPushKey = functions.database.ref('doors/{MACaddress}/inRoom');
    console.log(roomPushKey);  
    return snapshot.ref.parent.parent.child('rooms').child(roomPushKey).child('ins').set(timestamp);
});  

Note: I have fiddled with the code and I got it to run by changing the trigger to onWrite but like this I get an error message: "snapshot.val" is not a function...

exports.updateRoom = functions.database.ref('doors/{MACaddress}/ins').onWrite((snapshot, context) => {     
    const timestamp = snapshot.val();
    const roomPushKey = functions.database.ref('doors/{MACaddress}/inRoom');
    console.log(roomPushKey);
    return snapshot.ref.parent.parent.child('rooms').child(roomPushKey).child('ins').set(timestamp);
});  
Share Improve this question edited May 2, 2018 at 5:12 Peter Haddad 80.9k25 gold badges145 silver badges146 bronze badges asked May 1, 2018 at 14:46 Joao Alves MarruchoJoao Alves Marrucho 86911 silver badges32 bronze badges 3
  • I did. I will mark it as correct but my code is still not working. Could you point me into writing the the function correctly? I will make a new question. – Joao Alves Marrucho Commented May 1, 2018 at 16:33
  • is it still not working with onCreate? I explained below why it does not work with onWrite – Peter Haddad Commented May 1, 2018 at 16:34
  • yeah... the onCreate wasn't running... I have managed to do a little progress with your tips about the onWrite. But I would appreciate some help more: stackoverflow.com/questions/50120415/… – Joao Alves Marrucho Commented May 1, 2018 at 16:46
Add a comment  | 

1 Answer 1

Reset to default 20

If you are using onWrite, you have to do the following:

exports.dbWrite = functions.database.ref('/path').onWrite((change, context) => {
 const beforeData = change.before.val(); // data before the write
 const afterData = change.after.val(); // data after the write
});

onWrite is used when any changes happen in the specified path, so you can retrieve the before the changes and after the changes.

more info here:

https://firebase.google.com/docs/functions/beta-v1-diff#realtime-database

https://firebase.google.com/docs/reference/functions/functions.Change

In onCreate, you can do this:

exports.dbCreate = functions.database.ref('/path').onCreate((snap, context) => {
const createdData = snap.val(); // data that was created
});

Since onCreate is triggered when you add new data to the database.

发布评论

评论列表(0)

  1. 暂无评论