Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.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
Javascript 使用NodeJS和Axios迭代Mongo集合_Javascript_Node.js_Reactjs_Mongodb_Axios - Fatal编程技术网

Javascript 使用NodeJS和Axios迭代Mongo集合

Javascript 使用NodeJS和Axios迭代Mongo集合,javascript,node.js,reactjs,mongodb,axios,Javascript,Node.js,Reactjs,Mongodb,Axios,我希望通过一个集合循环,基本上捕获我创建的“诊所”文档中某个属性的objectID。它console.log正确的objectID,但由于某些原因,它没有运行this.setState,而是出错,说TypeError:this.setState不是函数。我不确定我在这里做错了什么-感谢所有的帮助 constructor(props){ super(props); this.onChangeName = this.onChangeName.bind(this); this

我希望通过一个集合循环,基本上捕获我创建的“诊所”文档中某个属性的objectID。它console.log正确的objectID,但由于某些原因,它没有运行this.setState,而是出错,说
TypeError:this.setState不是函数
。我不确定我在这里做错了什么-感谢所有的帮助

constructor(props){
    super(props);

    this.onChangeName = this.onChangeName.bind(this);
    this.onChangeEmail = this.onChangeEmail.bind(this);
    this.onChangeDOB = this.onChangeDOB.bind(this)
    this.onChangePhonenumber = this.onChangePhonenumber.bind(this);
    this.onChangeAddress = this.onChangeAddress.bind(this);
    this.onChangeFamilySize = this.onChangeFamilySize.bind(this);
    this.onChangePrescription = this.onChangePrescription.bind(this);
    this.onChangeClinicID = this.onChangeClinicID.bind(this);
    this.onSubmit = this.onSubmit.bind(this);

    this.state = {
        name: '',
        email: '',
        dob: new Date(),
        phonenumber: 0,
        address: '',
        familysize: '',
        prescription: '',
        clinicID: null,
        prescriptions: [],
    }
}


onSubmit(e){
    e.preventDefault();
    
    axios.get('http://localhost:5000/clinics/')
    .then(response => {
        for (var i = 0; i < response.data.length; i++){
            var clinicPrescription = response.data[i].prescription;
            //console.log(clinicPrescription)
            
            var n = clinicPrescription.localeCompare(this.state.prescription);
            if(n == 0){
                console.log(response.data[i]._id)
                this.setState({
                    clinicID: response.data[i]._id
                })
            } 
        
            
        }
    })
    .catch((error) => {
        console.log(error);
    })

    const patient = {
        name: this.state.name,
        email: this.state.email,
        dob: this.state.dob,
        phonenumber: this.state.phonenumber,
        address: this.state.address,
        familysize: this.state.familysize,
        prescription: this.state.prescription,
        clinicID: this.state.clinicID
    }

    console.log(patient)

    axios.post('http://localhost:5000/patients/add', patient)
        .then(res => console.log(res.data));

    this.setState = {
        name: '',
        email: '',
        dob: new Date(),
        phonenumber: 0,
        address: '',
        familysize: '',
        prescription: '',
        clinicID: null,

    }
    
    //window.location = '/matching';
    
}
构造函数(道具){
超级(道具);
this.onChangeName=this.onChangeName.bind(this);
this.onchangemail=this.onchangemail.bind(this);
this.onChangeDOB=this.onChangeDOB.bind(this)
this.onChangePhonenumber=this.onChangePhonenumber.bind(this);
this.onchangedAddress=this.onchangedAddress.bind(this);
this.onChangeFamilySize=this.onChangeFamilySize.bind(this);
this.onchangeportime=this.onchangeportime.bind(this);
this.onChangeClinicID=this.onChangeClinicID.bind(this);
this.onSubmit=this.onSubmit.bind(this);
此.state={
名称:“”,
电子邮件:“”,
dob:新日期(),
电话号码:0,
地址:'',
家庭化:,
处方:'',
clinicID:null,
处方:[],
}
}
提交(e){
e、 预防默认值();
axios.get()http://localhost:5000/clinics/')
。然后(响应=>{
对于(var i=0;i{
console.log(错误);
})
常数病人={
名称:this.state.name,
电子邮件:this.state.email,
多布:这个州,多布,
phonenumber:this.state.phonenumber,
地址:this.state.address,
familysize:this.state.familysize,
处方:这个,州,处方,
clinicID:this.state.clinicID
}
控制台日志(患者)
轴心柱http://localhost:5000/patients/add",病人)
。然后(res=>console.log(res.data));
this.setState={
名称:“”,
电子邮件:“”,
dob:新日期(),
电话号码:0,
地址:'',
家庭化:,
处方:'',
clinicID:null,
}
//window.location='/matching';
}

您正在覆盖函数this.setState

很抱歉,我应该在前面提到这一点,但我已经绑定了函数,但它仍然会给我该错误。问题可能在于
clinicID
持有objectID而不是字符串/数字吗?当然,只是将其添加到了原始帖子中。我包括了我的构造函数以及onSubmit代码的其余部分。您正在将setState设置为要覆盖的对象。函数是onSubmit中的一个函数吗?是的,onSubmit中有一个setState。我去掉了它,错误消失了,但在它发布后,clinicID在我的数据库中仍然为空。这可能是因为我在开始时将clinicID初始化为null?
constructor(props) {
  super(props);

  this.onChangeName = this.onChangeName.bind(this);
  this.onChangeEmail = this.onChangeEmail.bind(this);
  this.onChangeDOB = this.onChangeDOB.bind(this)
  this.onChangePhonenumber = this.onChangePhonenumber.bind(this);
  this.onChangeAddress = this.onChangeAddress.bind(this);
  this.onChangeFamilySize = this.onChangeFamilySize.bind(this);
  this.onChangePrescription = this.onChangePrescription.bind(this);
  this.onChangeClinicID = this.onChangeClinicID.bind(this);
  this.onSubmit = this.onSubmit.bind(this);

  this.state = {
    name: '',
    email: '',
    dob: new Date(),
    phonenumber: 0,
    address: '',
    familysize: '',
    prescription: '',
    clinicID: null,
    prescriptions: [],
  }
}

exportPatient() {
  return {
    name: this.state.name,
    email: this.state.email,
    dob: this.state.dob,
    phonenumber: this.state.phonenumber,
    address: this.state.address,
    familysize: this.state.familysize,
    prescription: this.state.prescription,
    clinicID: this.state.clinicID
  }
}

isCurrentClinic = clinic => {
  return clinic.prescription.localeCompare(this.state.prescription) === 0;
}

async setClinicID = () => {
  const url = 'http://localhost:5000/clinics/';
  const { data: clinics } = await axios.get(url);

  const clinic = clinics.find(clinic => this.isCurrentClinic(clinic));
  const clinicID = clinic && clinic._id;

  if (clinicID) {
    this.setState({ clinicID });
  }
}

async addPatient = () => {
  const url = 'http://localhost:5000/patients/add';
  const { data } = await axios.post(url, this.exportPatient())

  console.log(data);
}

async onSubmit(e) {
  e.preventDefault();

  try {
    await setClinicID();
    await addPatient();
  } catch (error) {
    console.log(error);
  }
}