我希望客户端应用程序将请求发送到后端,然后再对云数据库进行获取查询。我不知道怎么做的棘手部分是基于输入数据在查询上构建.where()
方法链。
成功构建方法链可能如下所示:
db
.collection('cities')
.get()
.where('storeId', '==', '1029')
.where('state', '==', 'CA')
.where('population, '<=', '100000')
.where('district', '==', 'purple');
// example of how I'd pass data to a wrapper method to build the above.
instance.getSome('1029', "cities", {
state : ["==", "CA"]
, population : ["<=", "100000"]
, district : ["==", "purple"]
})
是否有可能做到这一点?到目前为止,我只有这个,我不确定如何将字符串或类似内容转换为方法链
async getSome(storeId, collection, whereClauses) {
}
更新资料
根据以下答案,这是逐步的工作:
public async getSome(collection: string, whereClauses: object): Promise<object> {
const baseQuery = db
.collection(collection)
.get()
.where("storeId", "==", this.shopDomain);
const whereClause2dArray = Object.entries(whereClauses);
const stitchedQuery = whereClause2dArray
.reduce((accumulatorQuery, [column, [operation, value]]) => {
return accumulatorQuery.where(column, operation, value);
}, baseQuery);
return await stitchedQuery();
}
最佳答案
您可以执行以下操作:
async getSome(storeId, collection, whereClauses) {
let query = db
.collection(collection)
.get()
.where('storeId', '==', storeId);
query = Object.entries(whereClauses).reduce(
(query, [key, [operation, value]]) => query.where(key, operation, value)
, query);
return await query;
}
关于javascript - 如何从输入创建方法链?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59712376/