Issue
I have firebase (realtime database) simple structure like this: As the key value will be "unit no" .
My output screen as below : example , when search unit no "1-2" , able to list out the item filter by unit no .
when try to click "confirm recipient " to specific item , I want the remove the only record from firebase .
here is the problem facing :
1) undefine value for "item.unitno" anf "item.trakno" 2) TypeError: Cannot read property 'unitno' of undefined
html
<ion-row *ngFor="let item of result; let idx = index" class="text-content">
<ion-col class="table-cell" size="3">{{item.unitno}}</ion-col>
<ion-col class="table-cell" size="3">{{item.trackno}}</ion-col>
<ion-col class="table-cell" size="3">{{item.mydate | date: 'yyyy-MM-dd'}}</ion-col>
<button class="btn btn-primary btn-xs" (click)="receiveparcel(item)" size="3">Confirm by recipient</button>
TS
async receiveparcel(item) {
var db = firebase.database().ref('post/');
var query = firebase.database().ref('post/').orderByKey();
query.once("value").then(function(snapshot) {
snapshot.forEach(function(childSnapshot) {
var pkey = childSnapshot.key;
var chval = childSnapshot.val();
//check if remove this child
if(chval.unitno == item.unitno && chval.trackno == item.trackno){
console.log(item.unitno);
console.log(item.trackno);
console.log(pkey);
console.log(chval);
db.child("post/" +pkey).remove();
return true;
}
});
});
}
Latest :
sorry my mistake .forgot to save the html .now manage to resolve the type error however .the particular firebase record still not deleted even clicked the "receiveparcel" function.
After amend: result after amended . no error found however the record still not able to remove ?
Solution
The code looks correct expect you are using .remove()
which returns a promise without await
in a forEach
loop. You cannot use await
in a for each loop I'd recommend adding those delete operations into an object and then using the update
method as shown:
async receiveparcel(item) {
var db = firebase.database().ref('post/');
var query = firebase.database().ref('post/').orderByKey();
const snapshot = await query.once("value")
const updateObj = {}
snapshot.forEach(function (childSnapshot) {
var pkey = childSnapshot.key;
var chval = childSnapshot.val();
//check if remove this child
if(chval.unitno == item.unitno && chval.trackno == item.trackno) {
console.log(item.unitno, item.trackno);
console.log("pkey:", pkey, "& chval:", chval);
updateObj[pkey] = null
// don't return from within forEach loop
}
});
await db.update(updateObj)
return true
}
Answered By - Dharmaraj
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.