I want to filter of my snapshot but i sometimes i am getting [undefined], i dont want to return undefined inside, what is the issue?
return snapshot.docs.map((doc) => {
const jn = JSON.parse(doc.data().jsonData)
const res= jn.attributes.find(t => t.typet === tokenAttrs[0].name);
if(res){
return doc.data()
}
})
Many ways to solve it. One would be to use a foreach and push the new value to an new array. or you filter the empty values from your result before you send it back. like that:
const r = snapshot.docs.map((doc) => {
const jn = JSON.parse(doc.data().jsonData)
const res= jn.attributes.find(t => t.typet === tokenAttrs[0].name);
if(res){
return doc.data()
} else {
return null;
}
})
// then remove all empty values
return r.filter(n => n)
const res = [];
snapshot.docs.forEach((doc) => {
const jn = JSON.parse(doc.data().jsonData)
const res= jn.attributes.find(t => t.typet === tokenAttrs[0].name);
if (res){
res.push(doc.data());
}
})
return res;
May it can't find the attribute that match the condition,cause the res is undefined. You should check it out.
You can use the filter function this way:
return snapshot.docs.map(doc => {
const jn = JSON.parse(doc.data().jsonData);
const res = jn.attributes.find(t => t.typet === tokenAttrs[0].name);
if (res) return doc.data();
}).filter(_ => _ !== undefined);
This will remove the undefined values from the array.