0 votes

Angular and NodeJS get only part of the JSON I receive from the api.

I have this API on my server at nojs :

BACKEND

router.get('/all',usuarios_controller.obtener_todos);

controller_users.get_all

exports.obtener_todos = function(req, res) {
  User.find()
    .then(usuarios => {
      if (usuarios) {
        res.json({
          status: 200,
          usuarios: usuarios
        });
      }
    })
    .catch(err => {
      res.json({
        status: 404,
        message: "Error al obtener todos los usuarios",
        error: err
      });
    });
};

As you can see my API returns a status y usuarios the latter is a user collection .

FRONTEND

This function calls the function getAll() from usuarioService

  cargarUsuarios() {
    console.log("ABAJO");

     this.usuarioService.getAll().subscribe(
      data=>{
        this.usuarios = data;
        console.log('DATA API = ',data)
      },
      error=>{
        console.log('Error al listar los usuarios desde el servidor')
      }
    );
  }

Which returns the following json :

{…}
​
status: 200
​
usuarios: (3) […]
​​
0: {…}
​​​
__v: 0
​​​
_id: "5ca4865ec715f432d8802922"
​​​
apel_1: "xxxx"
​​​
apel_2: "xxxxx"
​​​
email: "xxxx@gmail.com"
​​​
fecha_nacimiento: "1995-04-22T22:00:00.000Z"
​​​
nombre: "xxxx angel"
​​​
<prototype>: Object { … }
​​
1: {…}
​​​
_id: "5ca4873849704f9cdd736893"
​​​
apel_1: "xxxx"
​​​
apel_2: "xxx"
​​​
email: "xxxx@gmail.com"
​​​
fecha_nacimiento: "1989-12-10T22:00:00.000Z"
​​​
nombre: "xxxx"
​​​
<prototype>: Object { … }
​​
2: {…}
​​​
_id: "5ca4873849704f9cdd736895"
​​​
apel_1: "xxxx"
​​​
apel_2: "xxxx"
​​​
email: "xxxx@gmail.com"
​​​
fecha_nacimiento: "1978-02-01T22:00:00.000Z"
​​​
nombre: "xxxxx"
​​​
<prototype>: Object { … }
​​
length: 3
​​
<prototype>: Array []
​
<prototype>: {…

In other words, a status and a array of users. Well, in this function

  cargarUsuarios() {
    console.log("ABAJO");

     this.usuarioService.getAll().subscribe(
      data=>{
        this.usuarios = data;
        console.log('DATA API = ',data)
      },
      error=>{
        console.log('Error al listar los usuarios desde el servidor')
      }
    );
  }

I'm having trouble getting the data within users the latter is of type public usuarios: Usuario[]; how could I access inside data and take only the array from Usuarios I have tried the following but it won't let me :

this.usuarios = data.usuarios

0voto

josanangel Points 455

I got what I wanted, I'm sharing it in case someone had a problem with it. similar problem :

  cargarUsuarios() {
    console.log("ABAJO");

     this.usuarioService.getAll().subscribe(
      data=>{
        //this.usuarios = data;
        if (data['status'] == 200) {
          let data_usuarios = data['usuarios'];
          this.usuarios = data_usuarios;
          console.log('DATA API = ',data)
        }
      },
      error=>{
        console.log('Error al listar los usuarios desde el servidor')
      }
    );
  }

with data[nombrePropiedad] you can access whatever you want

HolaDevs.com

HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by:

X