I am unable to return complete data with adonisjs

Asked

Viewed 16 times

0

Code of the Model

'use strict'

const honorarios = require('../Metodos/honorarios')

/** @type {typeof import('@adonisjs/lucid/src/Lucid/Model')} */
const Model = use('Model')

class ControleHonorario extends Model {

    saldo(){
        return this.belongsTo('App/Models/Saldo', 'saldoAnterior_id')
      }

    procurador(){
        return this.belongsTo('App/Models/User', 'procurador_user_id')        
        
    }

    remuneracao(){
        return this.belongsTo('App/Models/Remuneracao', 'remuneracao_id')        
        
    }

    teto(){
        return this.belongsTo('App/Models/Teto', 'teto_id')         
    }
  

    salario(){
        return this.belongsTo('App/Models/Salario')        
    }   

    
}


module.exports = ControleHonorario

Code of my control

async index ({ request, response }) {
        
const honorarios = await Honorario.query().with('procurador').with('teto').with('saldo').with('remuneracao').with('salario').fetch()
let honorarios_serializado = honorarios.toJSON()


   const retorno = honorarios_serializado.map(function(registro, indice){   
                   
      /*return {
         nome: registro.procurador.nome,
         teto:registro.teto.valor_teto,
         honorario_devidos: parseFloat(registro.honorarios_devidos),
         saldo_atual: parseFloat(registro.saldo.saldo_atual),
         saldo_anterior:"aguardando calculo",
         
      }*/
   })


   return honorarios_serializado
}

For now I generate this json

[
    {
        "id": 16,
        "honorarios_devidos": "13609.62",
        "data_referencia": "2020-09-02T00:00:00.000Z",
        "created_at": "2021-01-21 12:48:23",
        "updated_at": "2021-01-21 15:48:23",
        "user_id": 1,
        "procurador_user_id": 7,
        "teto_id": 2,
        "remuneracao_id": 75,
        "saldoAnterior_id": 12,
        "procurador": {
            "id": 7,
            "nome": "Pedro simão de andrade",
            "matricula": "4565456546",
            "email": "[email protected]\t",
            "password": "$2a$10$O3tEFGl1rXp7XrJCu2D13.rzvFtX2GrFWX09S5JCUMYN",
            "status": "1",
            "created_at": "2020-12-30 17:32:10",
            "updated_at": "2020-12-30 17:32:10",
            "grupo_id": 3
        },
        "teto": {
            "id": 2,
            "valor_teto": "39293.32",
            "status": "1",
            "created_at": "2020-12-30 17:34:06",
            "updated_at": "2020-12-30 17:34:06",
            "user_id": 1
        },
        "saldo": {
            "id": 12,
            "saldo_atual": "7510.08",
            "procurador_user_id": 7,
            "created_at": "2021-01-21 15:48:23",
            "updated_at": "2021-01-21 15:48:23"
        },belongsToMany
        "remuneracao": {
            "id": 75,
            "vant_pessoal": "1472.10",
            "data_referencia": "2020-09-01T00:00:00.000Z",
            "created_at": "2021-01-14 15:42:30",
            "updated_at": "2021-01-14 15:42:30",
            "user_id": 1,
            "procurador_user_id": 7,
            "salario_id": 2
        },
        "salario": null
    },

The problem is that I am not able to bring the salary according to (compensation.salario_id), because it is already a field of a relationship, ie it would be a "subquery".

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.