0

書き込みが完了した後(クラウド機能で)、値を更新しようとしていますが、動作しません(これは愚かな単純な問題です)。以下のコード:クラウドファンクションで書き込みが完了した時点で値を更新します

const functions = require('firebase-functions'); 
const admin = require('firebase-admin'); 
const firebase = require('firebase'); 

admin.initializeApp(functions.config().firebase); 
exports.createMessage = functions.https.onRequest((request, response) => { 
     const json = JSON.parse(request.query.json); // == "{'start':0, 'end':0}" 
     json.start = firebase.database.ServerValue.TIMESTAMP; 
     admin.database().ref('/messages/').push(json).then(snapshot => { 

      //Here is the problem. Whatever I try here it won't work to retrieve the value. 
      //So, how to I get the "start" value, which has been written to the DB (TIMESTAMP value)? 
      var startValue = snapshot.ref.child('start').val(); 

      snapshot.ref.update({ end: (startValue + 85800000) }).then(snapshot2=>{ 
       response.redirect(303, snapshot.ref); 
      }); 
     }); 
}); 

admin.database()を使用している問題はありますか?

答えて

0

私はFirebaseにバグがあるのか​​はわからないか、私はそれが間違って使用している場合、私はsnapshot - 基準上の任意のメソッドを呼び出そうと私は言ってエラーが発生します。TypeError: snapshot.xxx is not a function場所xxxは、私が使用しようとしている関数名です(例えば、子(...)、forEach(...)など)。

admin.database().ref('/messages/').push(json).once('value').then(snapshot => { 

の代わり:

しかし、次はsnapshotの問題を修正するようだ

admin.database().ref('/messages/').push(json).then(snapshot => { 

マイ国連推測があることpushためthen -promise、 -functionは何らかの障害を返しますsnapshotは動作するように思われるのはsnapshot.keyです。

私が間違っていないと、私の解決策ではなく2回の読み込みが行われますか? pushは書いてから書かれた値を(おそらく)読んで返すので、もう一度once(value)で読んでいます。

誰かがこの問題についてさらに深く洞察していますか?

2

このコード:

var startValue = snapshot.ref.child('start').val(); 

が実際にどの値を取得しません。 DataSnapshotのドキュメントをご覧ください。 child()でそのスナップショットに直接アクセスしてください。refは必要ありません。多分、これはあなたが意味するものですか?

var startValue = snapshot.child('start').val(); 
+0

ありがとうございますが、これは動作しません。実際、私はスナップショットのレフェレンスではどのメソッドも使用できません。エラーログ: 'TypeError:snapshot.childは関数ではありません'。私は私の問題を "解決"しました。詳細については私の答えを見てください。 – Whyser

関連する問題