【发布时间】:2020-11-01 14:30:24
【问题描述】:
addMoreCases = (e) => {
e.preventDefault();
this.setState({
makeActive10: false,
toggleDetails10: true,
});
const studentName = this.props.location.state.selectedStudent.basicData
.studentName;
var db = fire.firestore();
const casesObject = this.state.casesObject;
db.collection("students")
.where("basicData.studentName", "==", studentName)
.get()
.then(function (querySnapshot) {
querySnapshot.forEach(function (doc) {
console.log(doc.id, " => ", doc.data());
db.collection("students")
.doc(doc.id)
// .doc( )
.update(
{
...{
casesAndAssociatesData: {
...{ casesObject },
},
},
}
// { merge: true }
);
});
});
this.setState({
casesObject: [
// { ...initialCasesForm }
{
casesPoliceStation: "",
crimeNumber: "",
sectionOfLaw: "",
stage: "",
},
],
});
};
<Form onSubmit={this.addMoreCases}>
<Form.Row key={index}>
<Col>
<Form.Control
style={{ marginBottom: 20 }}
type="text"
onChange={(e) =>
this.casesObjectUpdate(e, index)
}
name="casesPoliceStation"
placeholder="Police Station"
value={cases.casesPoliceStation}
onKeyPress={(e) => {
e.key === "Enter" &&
e.preventDefault();
}}
/>
</Col>
<Col>
<Form.Control
style={{ marginBottom: 20 }}
type="text"
onChange={(e) =>
this.casesObjectUpdate(e, index)
}
name="crimeNumber"
placeholder="Crime Number"
value={cases.crimeNumber}
onKeyPress={(e) => {
e.key === "Enter" &&
e.preventDefault();
}}
/>
</Col>
<Col>
<Form.Control
style={{ marginBottom: 20 }}
type="text"
onChange={(e) =>
this.casesObjectUpdate(e, index)
}
name="sectionOfLaw"
placeholder="Section Of Law"
value={cases.sectionOfLaw}
onKeyPress={(e) => {
e.key === "Enter" &&
e.preventDefault();
}}
/>
</Col>
<Col>
<Form.Control
style={{ marginBottom: 20 }}
type="text"
onChange={(e) =>
this.casesObjectUpdate(e, index)
}
name="stage"
placeholder="Stage"
value={cases.stage}
onKeyPress={(e) => {
e.key === "Enter" &&
e.preventDefault();
}}
/>
</Col>
<Col>
<Button
type="submit"
variant="outline-primary"
>
在这里,在上面的代码中,我尝试在数组中添加更多地图,用于特定的文档 ID。因此,在按钮提交时,我将使用“addMoreCases”功能。它还应该在casesObject 数组的caseAndAssociatesData 字段中添加另一个映射。我将附上我的firestore数据库图像,在“casesAndAssociatesData”字段中,在“casesObject”数组中,我需要另一个带有表单值的映射,应该添加递增的Indexin the above image, if I enter the values in form and hit submit button, I need the values to be added in "casesObject" array with index "2" in "casasAndAssociatesData"
【问题讨论】:
标签: javascript arrays reactjs firebase google-cloud-firestore