I am trying to check whether a specific productID and orderID is in my database.
Currently, I am calling and fetching the data from my database like so.
const { data } = useFetchCollection("returns");
const filteredReturns = data.filter((mapper) => mapper.userID === userID);
const idReturns = filteredReturns.flatMap((mapper) => {
const getOrderID = mapper.orderID;
const getProductID = mapper.productID;
return [getOrderID, getProductID];
});
The data is being called and I am getting this array back:
['xxy6mkDDIhXQbUcol1Vh', '6RpJmOYLcGOkyAW4ElP9', 'JIzCbmOqp5wCGz7pHGSL', '6RpJmOYLcGOkyAW4ElP9']
I also have the orderID and productID from the user, these pieces of data will be in the database if a user has submitted a return and not in the database if the user has not submitted a return. So in other words it may or may not be in the database.
The orderID and the productID which is specific to the user is in this format for example,
userDetails[0].id = 6RpJmOYLcGOkyAW4ElP9
id = JIzCbmOqp5wCGz7pHGSL
I now want to check whether this data is in the database.
const mapData = idReturns.map((mapper) => {
if(mapper.orderID === userDetails[0].id && mapper.productID === id){
return true
}else {
return false
}
});
I have been using the function above but it doesn't seem to work. It doesn't say true or false properly. Notably, it only says true, false rather than definitively just true true.
I think my approach with my function is poor. I may even be making a mistake by doing this,
return [getOrderID, getProductID]~
but I am open to suggestions and advice.
Thanks in advance!
CodePudding user response:
use findIndex() instead of map()
if(idReturns.findIndex((mapper)=>mapper.orderID === userDetails[0].id && mapper.productID === id) !=-1) {
return true
}else{
return false
}