0

私はcomponentWillMountのFirebaseからJSONオブジェクトを取得する反応アプリケーションを構築しています。 Firebaseの呼び出しを行う非同期関数を設定しましたが、オブジェクトと一緒に返されるのではなく、コールバック関数自体が返されます。私は componentWillMountでfirebaseオブジェクトを返さない非同期関数

は、ここに私そして、getMessagesに非同期機能

import loadDB from './db'; 

// create a promise that returns snapshot.val 
export default async (keyProp) => { 
    try { 
    const db = await loadDB() 
    const key = db.ref('hashtags/' + keyProp) 

    const response = await key.on('value', function(snapshot) { 
     snapshot.val(); //Firebase Object displays in console.log here 
    }) 

    return response 
    } 
    catch(err) { 
    console.log('fetch failed', err); 
    return null 
    } 
} 

だとここで私は問題が関係していると思う私のcomponentWillMount

import getMessages from '../lib/get-messages.js' 

componentWillMount() { 
     const key = this.props.url.query.name 

     getMessages(key) 
      .then(function(messages){ 

      // messages displays function (snapshot) {snapshot.val()} 
      // in console.log instead of my firebase object 
      console.log('msgs', messages) 
      }) 
     } 

..です約束のビット混乱していると、いくつかの明確化が必要const responseこれはファイヤーベースオブジェクトを返さない。

答えて

0

以下の関数の値はreturnである必要があります。

const response = await key.on('value', function(snapshot) { 
    // snapshot.val(); --> this need to be returned 
    return snapshot.val(); 
}) 
+0

追加されました。しかし、まだ同じ応答を得ています。 – onehunnid

関連する問題