【问题标题】:Cloud Functions for Firebase database trigger?Firebase 数据库的 Cloud Functions 触发器?
【发布时间】:2017-10-18 19:34:16
【问题描述】:

知道为什么我不能使用 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)

【问题讨论】:

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


    【解决方案1】:

    它应该像这样工作:

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

    【讨论】:

    • 但这不是我想要构建数据的方式
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2017-12-13
      • 1970-01-01
      • 2018-08-06
      • 2017-10-02
      • 2017-08-25
      • 2017-10-01
      • 2021-11-17
      • 2018-05-21
      相关资源
      最近更新 更多