【问题标题】:Firebase Cloud Functions return wrong valueFirebase 云函数返回错误值
【发布时间】:2018-01-02 03:07:01
【问题描述】:

每次在我的 Firestore 数据库中发生特定写入时,我都会运行以下函数。计数器会按原样更新,如果我检查实时数据库中的值,它会显示正确的值。但是,如果我在 then() 中查询值,它总是返回 1。如果我将事务更改为总是将计数器增加 3,那么它返回 3。我做错了什么?

exports.someFunc = functions.firestore.document("/statistics/{userId}/postStats/{postId}/views/{user}/views/{view}").onCreate((event) => {
  const userId = event.params.userId
  const postId = event.params.postId

  return admin.database().ref(`/statistics/${userId}/${postId}/views`).transaction(function(current) {


         return current + 1;

       }).then(admin.database().ref(`/statistics/${userId}/${postId}/views`).once('value').then(function(snapshot) {

console.log(snapshot.val())

    );

});

【问题讨论】:

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


    【解决方案1】:

    我不太确定发生了什么。但是无论如何您都不必重新读取该值,因为它已作为参数传递给 Promise。得到它:

    exports.someFunc = functions.firestore.document("/statistics/{userId}/postStats/{postId}/views/{user}/views/{view}").onCreate((event) => {
      const userId = event.params.userId
      const postId = event.params.postId
    
      return admin.database().ref(`/statistics/${userId}/${postId}/views`).transaction(function(current) {
    
         return current + 1;
    
      }).then(function(committed, snapshot) {
    
         console.log(snapshot.val())
    
      );
    
    });
    

    【讨论】:

    • 这给了我以下错误:TypeError: Cannot read property 'val' of undefined at /user_code/index.js:106:27 at process._tickDomainCallback (internal/process/next_tick.js:135: 7)
    • 嗯....据我在文档中看到的承诺解析器的正确签名:firebase.google.com/docs/reference/admin/node/…
    猜你喜欢
    • 2019-08-21
    • 2019-09-03
    • 2019-07-30
    • 1970-01-01
    • 2018-12-03
    • 2019-08-12
    • 1970-01-01
    • 2020-05-16
    相关资源
    最近更新 更多