Reduce in Paginate produces an array inside an array

Reduce(
  Lambda(
    ['accumulator', 'ref'],
    Append(
      Get(Var('ref')),
      Var('accumulator')
    )
  ),
  [],
  Paginate(Documents(Collection('notifications')), {
    size: 2
  })
)

Will result to:

{
  after: [Ref(Collection("notifications"), "296811454570430977")],
  data: [
    [
      {
        ref: Ref(Collection("notifications"), "295665276532294149"),
        ts: 1619949211300000,
        data: {
          createdAt: "2021-04-12T11:33:50.342615Z",
          updatedAt: "2021-05-02T09:53:31.133472Z"
        }
      },
      {
        ref: Ref(Collection("notifications"), "295666276580196871"),
        ts: 1619943100860000,
        data: {
          createdAt: "2021-04-12T11:49:44.073589Z",
          updatedAt: "2021-05-02T08:11:40.677455Z"
        }
      }
    ]
  ]
}

Notice how data is an array containing an array.

The expected output is:

{
  after: [Ref(Collection("notifications"), "296811454570430977")],
  data: [
      {
        ref: Ref(Collection("notifications"), "295665276532294149"),
        ts: 1619949211300000,
        data: {
          createdAt: "2021-04-12T11:33:50.342615Z",
          updatedAt: "2021-05-02T09:53:31.133472Z"
        }
      },
      {
        ref: Ref(Collection("notifications"), "295666276580196871"),
        ts: 1619943100860000,
        data: {
          createdAt: "2021-04-12T11:49:44.073589Z",
          updatedAt: "2021-05-02T08:11:40.677455Z"
        }
      }
    ]
}

The reason I’m using Reduce is that in my app I’m doing Map and then Filter, I can combine those two loops by doing reduce but the output is unexpected.

It looks like the accumulator gets “upgraded” to a Page, since that’s what Paginate returns. That’s why the after cursor is part of the result.

The simplest change to make would be to use Select:

Reduce(
  Lambda(
    ['accumulator', 'ref'],
    Append(
      Get(Var('ref')),
      Var('accumulator')
    )
  ),
  [],
  Select(
    ["data"],
    Paginate(Documents(Collection('notifications')), { size: 2})
  )
)

Doing that drops the after cursor, so if you need to iterate over multiple pages, your original query would be better.