Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 从Firestore获取数据后如何设置状态_Reactjs_Firebase_Google Cloud Firestore - Fatal编程技术网

Reactjs 从Firestore获取数据后如何设置状态

Reactjs 从Firestore获取数据后如何设置状态,reactjs,firebase,google-cloud-firestore,Reactjs,Firebase,Google Cloud Firestore,我目前能够从Firestore获取用户数据,但在保存用户文档数据时遇到问题。我的控制台下面有个错误 TypeError: this.setState is not a function at Object.next (RepRequest.js:32) at index.cjs.js:1344 at index.cjs.js:1464 我试图从中跟踪另一个用户的问题 然而,仍然没有成功 在获得数据之后,我确实有一个两个api的请求,然后我就可以设置state了。我尝

我目前能够从Firestore获取用户数据,但在保存用户文档数据时遇到问题。我的控制台下面有个错误

  TypeError: this.setState is not a function
    at Object.next (RepRequest.js:32)
    at index.cjs.js:1344
    at index.cjs.js:1464
我试图从中跟踪另一个用户的问题 然而,仍然没有成功

在获得数据之后,我确实有一个两个api的请求,然后我就可以设置state了。我尝试将Firestore请求合并到承诺中,但未能成功,这就是我将其分离的原因。也许我走错了路,任何指导都是值得的

import React, { useEffect, useState } from "react";
import app from "./config/base.js";
import axios from "axios";

export default class RepRequest extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      userInfo: [],
      fedSens: [],
      fedReps: []
    };
  }

  componentDidMount() {
    const items = [];
    app.auth().onAuthStateChanged(function(user) {
      if (user) {
        console.log("User is signed in");
        let db = app
          .firestore()
          .collection("user")
          .doc(user.uid);
        db.get().then(doc => {
          if (doc.exists) {
            console.log("Document data:", doc.data());
            items.push(doc.data());
          } else {
            console.log("No doc exists");
          }
        });
      }
      this.setState({ userInfo: items });
    });

    Promise.all([
      axios.get(
        `https://api.propublica.org/congress/v1/116/senate/members.json`,
        {
          headers: { "X-API-Key": "9wGKmWl3kNiiSqesJf74uGl0PtStbcP2mEzSvjxv" }
        }
      ),
      axios.get(
        `https://api.propublica.org/congress/v1/116/house/members.json`,
        {
          headers: { "X-API-Key": "9wGKmWl3kNiiSqesJf74uGl0PtStbcP2mEzSvjxv" }
        }
      )
    ]).then(([rest1, rest2]) => {
      this.setState({
        fedSens: rest1,
        fedReps: rest2
      });
    });
  }

  render() {
    if (this.state.fedReps.length <= 0)
      return (
        <div>
          <span>Loading...</span>
        </div>
      );
    else {
      console.log(this.state.fedReps);
      return <div>test</div>;
    }
  }
}
import React,{useffect,useState}来自“React”;
从“/config/base.js”导入应用程序;
从“axios”导入axios;
导出默认类RepRequest扩展React.Component{
建造师(道具){
超级(道具);
此.state={
用户信息:[],
联邦调查局局长:[],
fedReps:[]
};
}
componentDidMount(){
常量项=[];
app.auth().onAuthStateChanged(函数(用户){
如果(用户){
console.log(“用户已登录”);
让db=app
.firestore()
.收集(“用户”)
.doc(user.uid);
db.get().then(doc=>{
如果(文件存在){
log(“文档数据:”,doc.data());
items.push(doc.data());
}否则{
console.log(“无单据存在”);
}
});
}
this.setState({userInfo:items});
});
我保证([
axios.get(
`https://api.propublica.org/congress/v1/116/senate/members.json`,
{
标题:{“X-API-Key”:“9WGKMWL3KNiISQESJF74UGL0PTSTBCP2MEZSVJV”}
}
),
axios.get(
`https://api.propublica.org/congress/v1/116/house/members.json`,
{
标题:{“X-API-Key”:“9WGKMWL3KNiISQESJF74UGL0PTSTBCP2MEZSVJV”}
}
)
]).然后([rest1,rest2])=>{
这是我的国家({
联邦调查局:rest1,
fedReps:rest2
});
});
}
render(){

如果(this.state.fedReps.length您的问题是由于混合了lambda函数声明(
(…)=>{…}
)和传统函数声明(
函数(…){}

lambda函数将从定义位置继承
,但传统函数的
将与定义位置的上下文隔离。这就是为什么在旧式兼容代码中经常看到
var self=this;
,因为
通常与您想要的不匹配

下面是演示此行为的示例片段:
函数doSomething(){
var anon=函数(){
console.log(this);/“this”独立于doSomething()
}
变量lambda=()=>{
console.log(this);//继承doSomething的“this”
}
lambda();//记录字符串“hello”
anon();//记录“窗口”对象
}

doSomething.call('hello')
İ我想你应该检查一下你的{和}谢谢你,我用了你的第一个选项,它成功了!我现在更明白了。