Empty return when adding an object to_.transform in Lodash

Viewed 36

I'm using Lodash to do some work on our object. So I had a little problem that I can't solve.

I'm trying to add the result of a query, in the _.transform array. But without success.

The return is empty.

I did some tests using the console.log and the value is shown normally.

const result = _.transform(data.items, async  (r: any, v: any) => {
      let data = await Cliente.query().where('id', '=', v.codiPsv).orderBy('validade', 'asc').first()
      if (data){
        console.log(data.serialize()) --> Show object
        r.push(data.serialize())
        console.log(0)
      }
    })

    console.log(result)   --> empty
1 Answers

Resolved

    const cods = _.map(data.items, 'codiPsv')
    const items = await Cliente.query().whereIn('id', cods).orderBy('validade', 'desc')
    const itemsSerelialize = items.map((item) => item.serialize())

    const result = _.transform(data.items, async (r: any, v: any) => {
      const item = _.filter(itemsSerelialize, {'id':v.codiPsv}).slice(-1).pop()
      if ( !_.isUndefined( item )) {
        r.push(item)
      } else {
        console.log(v)
        r.push({'local': 'Indisponível', ...v})
      }
    })
Related