Firebase云函数返回错误的值

Firebase云函数返回错误的值,firebase,firebase-realtime-database,google-cloud-functions,google-cloud-firestore,firebase-admin,Firebase,Firebase Realtime Database,Google Cloud Functions,Google Cloud Firestore,Firebase Admin,每当Firestore数据库中发生某种写入时,我都会运行以下函数。计数器会像应该的那样得到更新,而且如果我检查实时数据库中的值,它会显示正确的值。但是,如果我在my then()中查询该值,它总是返回1。如果我将我的事务更改为始终将计数器增加3,那么它将返回3。我做错了什么 exports.someFunc=functions.firestore.document(“/statistics/{userId}/postStats/{postId}/views/{user}/views/{view}

每当Firestore数据库中发生某种写入时,我都会运行以下函数。计数器会像应该的那样得到更新,而且如果我检查实时数据库中的值,它会显示正确的值。但是,如果我在my then()中查询该值,它总是返回1。如果我将我的事务更改为始终将计数器增加3,那么它将返回3。我做错了什么

exports.someFunc=functions.firestore.document(“/statistics/{userId}/postStats/{postId}/views/{user}/views/{view}”)。onCreate((事件)=>{
const userId=event.params.userId
const postId=event.params.postId
返回admin.database().ref(`/statistics/${userId}/${postId}/views`)。事务(函数(当前){
返回电流+1;
}).then(admin.database().ref(`/statistics/${userId}/${postId}/views`)。once('value')。然后(函数(快照){
console.log(snapshot.val())
);
});

我不太确定发生了什么。但无论如何,您都不必重新读取该值,因为它已作为参数传递到承诺中。使用以下方法获取该值:

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:无法读取进程中未定义的at/user\u code/index.js:106:27的属性“val”。\u tickDomainCallback(internal/process/next\u tick.js:135:7)Hmmm…。据我在文档中看到,这是承诺解析程序的正确签名: