Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何从输入创建方法链?_Javascript_Typescript - Fatal编程技术网

Javascript 如何从输入创建方法链?

Javascript 如何从输入创建方法链?,javascript,typescript,Javascript,Typescript,我希望客户端应用程序向后端发送请求,然后后端对云数据库执行get查询。我不知道如何做的一个棘手的部分是基于输入数据在查询上构建一个.where()方法链 成功构建的方法链可能如下所示: db .collection('cities') .get() .where('storeId', '==', '1029') .where('state', '==', 'CA') .where('population, '<=', '100000') .where('distri

我希望客户端应用程序向后端发送请求,然后后端对云数据库执行get查询。我不知道如何做的一个棘手的部分是基于输入数据在查询上构建一个
.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) {
  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;
}
更新

根据下面的答案,这是一个逐步进行的工作:


  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();
  }

public-async-getSome(集合:string,whereClauses:object):Promise{
const baseQuery=db
.收集(收集)
.get()
.where(“storeId”,“==”,this.shopDomain);
常量whereClause2dArray=Object.entries(whereClauses);
常量stitchedQuery=whereClause2dArray
.reduce((累加器查询,[列,[操作,值]])=>{
返回AccumeratorQuery.where(列、操作、值);
},baseQuery);
返回等待查询();
}

您可以执行以下操作:

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) {
  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;
}

您可以这样做:

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) {
  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;
}

您有两种不同的查询样式。哪个更重要?您尝试了什么?示例调用将在我自己的方法上,然后构建第一个,如果不清楚,很抱歉您可以在这里查看:您有两种不同的查询样式。哪个更重要?您尝试了什么?示例调用将在我自己的方法上,然后构建第一个,如果不清楚,请抱歉您可以在这里查看:查看
Object.entries()
,谢谢。一旦我测试了这个问题,我将投票并标记为最佳答案。看起来像是在jsbin.com的一个空类中复制/粘贴这个问题,我在
return wait query上得到一个SyntaxError
你知道为什么吗?@SeanDezoysa因为既然是这样,
getSome
应该在一个类体中,要声明它是这样的,你需要使用
异步函数getSome(…){…}
,但它不会像现在这样工作,因为没有
db
。如果可以的话,还有几个问题:1。我的逐步示例(上面)看起来正确吗?2.您和我的示例是否在定义基本查询的第3行上运行不可用的数据库调用?3.我在您的最后一行添加了()以实际调用附加查询,对吗?@seandezaysa我不知道,因为我不知道
db
是什么,我认为您不需要
()
,尝试删除它,然后
等待
,尝试使用
返回查询
现在查看
Object.entries()
谢谢。一旦我测试了这个问题,我将投票并标记为最佳答案。看起来像是在jsbin.com的一个空类中复制/粘贴这个问题,我在
return wait query上得到一个SyntaxError
你知道为什么吗?@SeanDezoysa因为既然是这样,
getSome
应该在一个类体中,要声明它是这样的,你需要使用
异步函数getSome(…){…}
,但它不会像现在这样工作,因为没有
db
。如果可以的话,还有几个问题:1。我的逐步示例(上面)看起来正确吗?2.您和我的示例是否在定义基本查询的第3行上运行不可用的数据库调用?3.我在您的最后一行添加了()以实际调用附加查询,对吗?@seandezaysa我不知道,因为我不知道
db
是什么,我认为您不需要
()
,尝试删除它,然后
等待
,尝试使用
返回查询