简体   繁体   English

如何将查询的数据转换为数组 FaunaDB 打字稿

[英]How to transform queried data to an array FaunaDB typescript

Simple question here: I could display the result of my pagination query (FaunaDB,FQL) in the console and it appears as a javascript object.这里有一个简单的问题:我可以在控制台中显示我的分页查询(FaunaDB,FQL)的结果,它显示为一个 javascript 对象。 Yet, I cannot access the properties of said object and even less can I convert it to an array using the spread operator.但是,我无法访问所述对象的属性,更不能使用扩展运算符将其转换为数组。 How could I do that?我怎么能那样做?

I am aware there exists a pagination helper but could not make it work, as explained above.我知道存在一个分页助手,但无法使其工作,如上所述。 Here is the latest code I am trying:这是我正在尝试的最新代码:

var array=[]
qu(q.Map(
    q.Paginate(q.Match(q.Index('FeesByIndex'))),
    q.Lambda(x => q.Get(x))
   )).then(res => { console.log(res); array=[...res] })//the log really looks like a js object and res is said to be one
  

It says type object is not an array type.它说类型对象不是数组类型。 Also, property data is said not to exist on res, although it clearly does in the console此外,据说属性数据不存在于 res 中,尽管它显然存在于控制台中

would you try this way?你会尝试这种方式吗?

var array=[]
  qu(q.Select(['data'],
      q.Map(
          q.Paginate(q.Match(q.Index('FeesByIndex'))),
          q.Lambda(x => q.Get(x))
      )
    )
  ).then(res => { console.log(res); array=[...res] })

You missed to specify the index term and the Lambda has syntax errors.您错过了指定索引项,并且 Lambda 存在语法错误。

The response object has a data property, which is a list.响应对象有一个数据属性,它是一个列表。

In my repositories I use this snippet if the query returns multiple object:在我的存储库中,如果查询返回多个对象,我将使用此代码段:

    const result: Fee[] = [];

    await CLIENT.query(
      q.Map(
        q.Paginate(
          q.Match(
            q.Index('FeesByIndex'),
            'index term',
          ),
        ),
        q.Lambda('fees', q.Get(q.Var('fees'))),
      )
    )
    .then(faunaResponse => {
      const dataArray = faunaResponse.data;
      dataArray.forEach(s => {
        const data = s.data;
        result.push({
          id: data.id,
          yourProp1: data.yourProp1,
          yourProp2: data.yourProp2,
        });
      });
    })
    .catch(e => logger.error(e));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM