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 그리고 firebase 객체를 반환하지 않습니다.

답변

0

아래 함수에서 return 값이 필요합니다 :

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

추가했습니다. 그래도 여전히 같은 반응을 얻고 있습니다. – onehunnid

관련 문제