Firebase数据库云功能触发器?

时间:2017-05-18 20:14:58

标签: firebase firebase-realtime-database google-cloud-functions

知道我为什么不能使用childByAutoId?

exports.addPersonalRecordHistory = functions.database.ref('/personalRecords/{userId}/current/{exerciseId}')
    .onWrite(event => {

       var path = 'personalRecords/' + event.params.userId + '/history/' + event.params.exerciseId;
       var reference = admin.database().ref(path).childByAutoId();

       reference.set({
            username: "asd",
            email: "asd"
          });
  });

错误

TypeError: admin.database(...).ref(...).childByAutoId is not a function
    at exports.addPersonalRecordHistory.functions.database.ref.onWrite.event (/user_code/index.js:18:111)

2 个答案:

答案 0 :(得分:5)

childByAutoId()适用于iOS SDK。对于admin.Database(),请使用push()

var reference = admin.database().ref(path).push();

答案 1 :(得分:0)

它应该像这样工作:

exports.addPersonalRecordHistory = functions.database.ref('/personalRecords/{userId}/current/{exerciseId}').onWrite(event => {
  var path = 'personalRecords/' + event.params.userId + '/history/' + event.params.exerciseId;
   return admin.database().ref(path).set({
    username: "asd",
    email: "asd"
  });
});