I’ve a list of _id values over a collection, and would like to retrieve an array of matching items. I’ve tested two ways that seem to work, and want to run them by everyone here. I prefer the First way, but am not 100% certain this is proper usage of .hasSome(…).
This is all backend code.
Any feedback, critique, or ideas regarding robustness, correctness, better ways?
First way (seems better):
function query_match_each (collection, fieldname, values) {
// generic service query, expect no more than one match for each item in values[]
return wixData.query(collection)
.hasSome(fieldname, values)
.limit(values.length)
.find({"suppressAuth": true, "suppressHooks": true })
.then(results => {
return results.items;
})
.catch(err =>{
log("query_match_each error",err);
return { "error" : err };
});
}
export function retrieve_each_store_order (idlist) {
// let the wixData query do all the work?
var ids = idlist.map(obj => { return obj.orderId; });
return query_match_each("Stores/Orders", "_id",ids);
}
Second way:
function query_exact_match (collection, fieldname, value) {
return wixData.query(collection)
.eq(fieldname,value)
.limit(100)
.find({"suppressAuth": true, "suppressHooks": true })
.then(results => {
return results.items;
})
.catch(err =>{
log("query_exact_match error",err);
return { "error" : err };
});
}
export function retrieve_matching_store_orders (idlist) {
// tricky async stuff w Promise api...
// everything in jsw returns a promise,
// to iterate a loop of calls to said func
// we have to accumulate all the promises
// and then call Promies.all(promises)
var promises = idlist.map(obj => {
return query_exact_match("Stores/Orders", "_id", obj.orderId)
});
return Promise.all(promises)
.then(results => { return results; })
.catch(err => { return { "error" : err }; });
}
Thank you for your time.
-Jennifer