How to use getManyAndCount() with typeorm, I am getting null

Viewed 3064

I am trying to get many rows and the count at the same time, so i am using getManyAndCount() this returns the data, but there is a relationship with another entity which I want to get, but this returns null

I tryied with getRawMany() which returns all the data that I need, but I don't get the count. So I am trying the next code

  cobranza = await cobranzaRepository
    .createQueryBuilder('cobranzas')
    .leftJoinAndSelect('cobranzas.alumno', 'alumno', 'alumno.nombre like :name', {
      name: '%' + req.query.alumno.toUpperCase() + '%',
    })
    .select([
      'cobranzas.id',
      'cobranzas.tipopago',
      'cobranzas.importepagado',
      'cobranzas.periodo',
      'cobranzas.createdAt',
      'cobranzas.recibo',
    ])
    .addSelect(['alumno.nombre', 'alumno.apellidopaterno', 'alumno.apellidomaterno'])
    .skip(req.query.skip)
    .take(req.query.per_page)
    .getManyAndCount();

I expect

    {
        "id": 22,
        "tipopago": 1,
        "importepagado": "500",
        "periodo": "SEM-11-2019",
        "createdAt": "2018-12-13T00:52:36.338Z",
        "alumno": {
            "id": 6,
            "nombre": "MARTIN",
            "apellidopaterno": "LOPEZ",
        }
    }

But the actual output is

    {
        "id": 22,
        "tipopago": 1,
        "importepagado": "500",
        "periodo": "SEM-11-2019",
        "createdAt": "2018-12-13T00:52:36.338Z",
        "alumno": null
    }
1 Answers

I know it is an old question, but if someone comes here like I did after all these years

I am not an expert but GetManyAndCount will only work with just the repository that you are currently on, typeorm can't map it to the Entities which you have defined, because there might be aliases in the select. So you have to get that data as raw data

also skip and take only works with getMany and getManyAndCount,

Related