简体   繁体   English

在 useEffect 中发出两个请求

[英]Making two requests in useEffect

I am trying to fetch some user data from Firebase using getDoc and some data from MongoDB using axios in React.js.我正在尝试使用 getDoc 从 Firebase 获取一些用户数据,并在 React.js 中使用 axios 从 MongoDB 获取一些数据。

Code:代码:

async function getSolvedProblems() {
    const docRef = await doc(db, "users-progress", user.uid);
    await getDoc(docRef).then((doc) => {
      console.log(doc.data());
    });
  }

  useEffect(() => {
    //fetch user's solved problems from firebase
    getSolvedProblems();

    //fetch problems from db server
    axios
      .get(process.env.REACT_APP_BACKEND_URL)
      .then((res) => {
        //doing something here
      })
      .catch((err) => {
        console.log(err);
      });
  }, []);

But I don't know why the firebase data is not getting logged in console, when I hit refresh.但我不知道为什么当我点击刷新时 firebase 数据没有登录控制台。 But when I make any change in code, and save it, then it gets logged.但是当我对代码进行任何更改并保存它时,它就会被记录下来。 I am unable to understand how useEffect is working here.我无法理解 useEffect 是如何在这里工作的。

This is use effect work on below:这是下面的使用效果工作:

useEffect(() => {
    //Runs only on the first render
}, []);

Also, you need to handle the catch block in your getSolvedProblems() method, see is there any error there.此外,您需要在 getSolvedProblems() 方法中处理 catch 块,看看那里是否有任何错误。

On my guess, there is no value on user.uid when you load on page render根据我的猜测,当您在页面渲染上加载时, user.uid 没有任何价值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM