簡體   English   中英

使用 NodeJS 和 Axios 遍歷 Mongo 集合

[英]Iterating through Mongo Collection Using NodeJS and Axios

我正在尋找循環遍歷一個集合,該集合基本上捕獲了我創建的“診所”文檔中屬性的 objectID。 它 console.logs 正確的 objectID 但由於某種原因它沒有運行 this.setState 而是錯誤地說TypeError: this.setState is not a function 我不確定我在這里做錯了什么 - 任何和所有的幫助表示贊賞!

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';
    
}
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);
  }
}

您正在覆蓋 function this.setState

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM