Reputation: 5
I'm looking to loop through a collection basically capture the objectID of a property in the "clinics" document I have created. It console.logs the proper objectID but for some reason it isn't running the this.setState and instead errors out saying TypeError: this.setState is not a function
. I am not sure what I am doing wrong here - any and all help appreciated!
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';
}
Upvotes: 0
Views: 145
Reputation: 3462
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);
}
}
You are overwriting the function this.setState
Upvotes: 1