ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 오류 : 백 오프 작업이 이미 진행 중입니다. React 앱에서 Firebase에서 데이터를 읽으려고 할 때
    카테고리 없음 2020. 8. 10. 10:00

    질문

    야, 방금 electron js를 시작하고 firebase에 연결하려고 시도했습니다. typescript에서 react를 사용하고 있습니다.

    firebase에서 데이터를 읽으려고 할 때 오류가 발생합니다.

    다음 링크 https://firebase.google.com/의firebase문서에서접근방식을시도했습니다.docs/firestore/quickstart

    Error: A backoff operation is already in progress.
        at ExponentialBackoff.backoffAndWait (D:\projects\vugha\Membership\app\node_modules\@google-cloud\firestore\build\src\backoff.js:164:35)
        at Firestore._retry (D:\projects\vugha\Membership\app\node_modules\@google-cloud\firestore\build\src\index.js:977:31)
    Caused by: Error
        at Firestore.getAll (D:\projects\vugha\Membership\app\node_modules\@google-cloud\firestore\build\src\index.js:755:23)
        at DocumentReference.get (D:\projects\vugha\Membership\app\node_modules\@google-cloud\firestore\build\src\reference.js:199:32)
        at Home._this.loginClick (http://localhost:1212/dist/renderer.dev.js:1355:40)
        at HTMLUnknownElement.callCallback (http://localhost:1212/dist/renderer.dev.js:2564:14)
        at Object.invokeGuardedCallbackDev (http://localhost:1212/dist/renderer.dev.js:2613:16)
        at invokeGuardedCallback (http://localhost:1212/dist/renderer.dev.js:2668:31)
        at invokeGuardedCallbackAndCatchFirstError (http://localhost:1212/dist/renderer.dev.js:2682:25)
        at executeDispatch (http://localhost:1212/dist/renderer.dev.js:2765:3)
        at executeDispatchesInOrder (http://localhost:1212/dist/renderer.dev.js:2790:5)
        at executeDispatchesAndRelease (http://localhost:1212/dist/renderer.dev.js:5654:5)

    그러나 나는 firebase에 데이터를 쓸 수 있습니다.

    /* eslint-disable promise/always-return */
    /* eslint-disable @typescript-eslint/ban-types */
    /* eslint-disable react/destructuring-assignment */
    /* eslint-disable react/no-unused-state */
    /* eslint-disable prettier/prettier */
    import React from 'react';
    import { Link } from 'react-router-dom';
    import routes from '../constants/routes.json';
    import styles from './Home.css';
    
    const admin = require('firebase-admin');
    const serviceAccount = require('../marinamembers-dd141799d80d.json');
    
    if (!admin.apps.length) {
      admin.initializeApp({
        credential: admin.credential.cert(serviceAccount),
      });
    }
    
    class Home extends React.Component<{},any>{
    
      constructor(props) {
        super(props);
        this.state = {
          username: '',
          password: '',
          alertText: '',
        };
      }
    
     loginClick = () => {
    
      const db = admin.firestore();
    
      db.collection('users').doc('hi').get().then((userData) => {
        console.log(userData.data);
      }).catch((e) => {
        console.log(e);
      });
     }
    
      render(){
        let inputStyle = { opacity: '0' };
    
        if(this.state.alertText !== ''){
          inputStyle = { opacity: '1' };
        }
    
        return (
          <div className={styles.home}>
            <div style={inputStyle} className={styles.alert}>
              <div>{this.state.alertText}</div>
              <span>&times;</span>
            </div>
            <div className={styles.main}>
              <div className={styles.container} data-tid="container">
                <h2>Home</h2>
                <Link to={routes.COUNTER}>by Vugha Technologies</Link>
              </div>
              <div className={styles.login} data-tid="login">
                <h2>Login</h2>
                <input onChange={(event) => {this.setState({username: event.target.value});}} type="text" id="loginName" name="loginName" placeholder="User Name" />
                <input onChange={(event) => {this.setState({password: event.target.value});}} type="password" id="loginPassword" name="loginPassword" placeholder="Password" />
                <button onClick={this.loginClick} type="button">Login</button>
              </div>
            </div>
          </div>
        );
      }
    }
    
    export default Home;
    

    답변1

    코드에서 읽을 때 await 키워드를 지정하지 않는 것을 알 수 있습니다.
    사실 문서 의 모든 메소드에는 await 키워드.
    이 키워드는 비동기 함수 내에서 사용되며 JavaScript가 Promise이 해결되고 결과를 반환 할 때까지 기다리게한다는 것을 이해합니다.

    Firestore에서 읽기 위해 공유 한 것과 동일한 지침을 따랐지만 Cloud 함수를 사용했으며 여기에 내 코드가 있습니다. 나는 그것이 약간의 빛을 비출 수 있기를 바랍니다.

    const admin = require('firebase-admin');
    admin.initializeApp();
    
    const db = admin.firestore();
    
    exports.readFirestore101 = functions.https.onRequest(async (request, response) => {
      console.log('Reading firestore starting...');
      const snapshot = await db.collection('users').get();
      snapshot.forEach((doc) => {
        console.log(doc.id, '=>', doc.data());
         });
      console.log('Reading firestore ending');
      response.status(200).send('Please see logs for results');
    });


     

     

     

     

    출처 : https://stackoverflow.com/questions/63245237/error-a-backoff-operation-is-already-in-progress-when-i-try-to-read-data-from-f

    댓글

Designed by Tistory.