Search code examples
javascripttypescriptfacebook-fqlfaunadb

How to transform queried data to an array FaunaDB typescript?


I could display the result of my pagination query (FaunaDB,FQL) in the console and it appears as a javascript object. 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


Solution

  • You missed to specify the index term and the Lambda has syntax errors.

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