【发布时间】:2019-11-03 16:27:12
【问题描述】:
我正在将一组数据插入到 firebase 数据库中。我遇到的问题是,我必须在此迭代代码中调用 Firebase 数据库,结果是虽然为每个对象值成功创建了一行,但输入每行的值始终相同(对象数组中的最后一个值)。
这个方法我已经试过了,还是不行:https://stackoverflow.com/a/42343907/1014250
submitted() {
if (!this.nestedForm.valid) {
alert('Please fill the forms');
} else {
this.id = this.generateUUID();
const id = this.id;
const permitnumber = this.nestedForm.value.permitnumber;
const permitdate = this.nestedForm.value.permitdate;
const incomingdate = this.nestedForm.value.incomingdate;
const storename = this.nestedForm.value.storename;
const explosivesarr = this.nestedForm.value.address;
const detonator = this.nestedForm.value.detonator;
const accessories = this.nestedForm.value.accessories;
console.log("explosivesarr"+explosivesarr)
explosivesarr.forEach(async element => {
this.explosivename = element.explosive;
this.qty = element.qty;
this.unit = element.unit;
this.product = this.productDetails.find(o => o.productname === element.explosive)
if(this.product){
const productid = this.product.id;
this.quantityinstore = this.product.totalquantity;
this.totalquantity = Number.parseFloat(this.quantityinstore.toString()) + Number.parseFloat(this.qty.toString());
this.db.createincomingoperation(id,this.uuid, permitnumber, permitdate,incomingdate ,
storename,explosivesarr,detonator,accessories).then(() => {
this.db.updateincomingoperation(productid,this.totalquantity)
.then(() => {
alert('incoming operation added successfully');
});
});
}
else if(!this.product){
this.quantityinstore = 0;
this.totalquantity = Number.parseFloat(this.quantityinstore.toString()) + Number.parseFloat(this.qty.toString());
await this.db.createincomingoperation(id,this.uuid, permitnumber, permitdate,incomingdate,
storename,explosivesarr,detonator,accessories).then(() => {
this.db.insertincomingoperation(
id,
this.explosivename,
this.qty,
this.unit,
permitnumber,
permitdate,
incomingdate,
storename,
this.totalquantity)
.then(() => {
alert('incoming operation added successfully');
this.id = this.generateUUID();
});
});
}
});
}
}
数据库服务
createincomingoperation(id,uuid, permitnumber, permitdate,incomingdate ,storename,explosivesarr,detonator,accessories): Promise<void> {
const dateTime = new Date().toISOString();
const status = 'On Delivery';
const color = '#FF8C00'; //orange
return this.db.doc(`productmeterials_logs/${id}`).set({
id,uuid,permitnumber,permitdate,incomingdate, storename, explosivesarr,
detonator,
accessories,
color,
dateTime,
status,
});
}
updateincomingoperation(id,totalquantity): Promise<void> {
return this.db.doc(`productmeterials/${id}`).update({
totalquantity
});
}
insertincomingoperation(
id,
productname,
qty,
unit,
permitnumber,
permitdate,
incomingdate,
storename,
totalquantity): Promise<void> {
const date: string = new Date().toISOString();
const method = 'incoming';
return this.db.doc(`productmeterials/${id}`).set({
id,
method,
productname,
qty,
unit,
permitnumber,
permitdate,
incomingdate,
storename,
totalquantity
});
}
不是在等待数据插入。
【问题讨论】:
标签: angular firebase async-await angular-promise