0
votes

I have these relations: https://pastebin.com/hMZRJe0S

and I need to get the ui given the user

using java script I tried to get the id of usuario, then the ids of rol, then the ids of funcion and finally the ius. By getting the matching elements and then converting the ids into an array to get the elements of the next table(or collection)

https://pastebin.com/JbbgB27P

fox example this is how i get the ids of rol:

const dbo = db.db("tareas");
    dbo
      .collection("usuario")
      .find({ nombre: name, password })
      .toArray(function(err, usuarios) {
        console.log(2);
        if (err) throw err;
        console.log(usuarios);
        if (usuarios.length > 0) {
          var x;
          dbo
            .collection("usuario_rol")
            .find({ "_id.id_usuario": usuarios[0]._id })
            .toArray(function(err, usuarios_rol) {
              if (err) throw err;
              var idroles = [];
              for (x = 0; x < usuarios_rol.length; x++) {
                idroles.push(usuarios_rol[x]._id.id_rol);
              }

then I get this "Topology was destroyed"

C:\Users\NORMA\Desktop\EXPRESS_CRASH_COURSE\node_modules\mongodb\lib\utils.js:132 throw err; ^

MongoError: Topology was destroyed at initializeCursor (C:\Users\NORMA\Desktop\EXPRESS_CRASH_COURSE\node_modules\mongodb-core\lib\cursor.js:596:25) at nextFunction (C:\Users\NORMA\Desktop\EXPRESS_CRASH_COURSE\node_modules\mongodb-core\lib\cursor.js:456:12) at Cursor.next (C:\Users\NORMA\Desktop\EXPRESS_CRASH_COURSE\node_modules\mongodb-core\lib\cursor.js:766:3) at Cursor._next (C:\Users\NORMA\Desktop\EXPRESS_CRASH_COURSE\node_modules\mongodb\lib\cursor.js:216:36) at fetchDocs (C:\Users\NORMA\Desktop\EXPRESS_CRASH_COURSE\node_modules\mongodb\lib\operations\cursor_ops.js:217:12) at toArray (C:\Users\NORMA\Desktop\EXPRESS_CRASH_COURSE\node_modules\mongodb\lib\operations\cursor_ops.js:247:3) at executeOperation (C:\Users\NORMA\Desktop\EXPRESS_CRASH_COURSE\node_modules\mongodb\lib\utils.js:416:24) at Cursor.toArray (C:\Users\NORMA\Desktop\EXPRESS_CRASH_COURSE\node_modules\mongodb\lib\cursor.js:829:10) at C:\Users\NORMA\Desktop\EXPRESS_CRASH_COURSE\routes\api\members.js:75:18 at result (C:\Users\NORMA\Desktop\EXPRESS_CRASH_COURSE\node_modules\mongodb\lib\utils.js:410:17) [nodemon] app crashed - waiting for file changes before starting...

1

1 Answers

0
votes
Try with this code working fine getting result

var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/";
 MongoClient.connect(url, function(err, db) {
     name="juan";
     password="juan123"
    console.log(1);
    if (err) throw err;
    const dbo = db.db("tareas");
    dbo
      .collection("usuario")
      .find({ nombre: name, password })
      .toArray(function(err, usuarios) {
        console.log(2);
        if (err) throw err;
        console.log(usuarios);
         if (usuarios.length > 0) {
          var x;
          dbo
            .collection("usuario_rol")
            .find({ "_id.id_usuario": usuarios[0]._id })
            .toArray(function(err, usuarios_rol) {
              if (err) throw err;
              var idroles = [];
              for (x = 0; x < usuarios_rol.length; x++) {
                idroles.push(usuarios_rol[x]._id.id_rol);
              }
                console.log(idroles);
              dbo
                .collection("rol_funcion")
                .find({ "_id.id_rol": { $in: idroles } })
                .toArray(function(err, roles_fun) {
                  if (err) throw err;
                    var idfunciones = [];
                  console.log(roles_fun);
                  for (x = 0; x < roles_fun.length; x++) {
                    idfunciones.push(roles_fun[x]._id.id_funcion);
                  }
                  dbo
                    .collection("funcion_iu")
                    .find({ "_id.id_funcion": { $in: idfunciones } })
                    .toArray(function(err, fun_iu) {
                      if (err) throw err;
                      var idius = [];
                      for (x = 0; x < fun_iu.length; x++) {
                        idius.push(fun_iu[x]._id.id_iu);
                      }
                      dbo
                        .collection("iu")
                        .find({ _id: { $in: idius } })
                        .toArray(function(err, ius) {
                            if (err) throw err;
                            console.log(ius);

                           });
                         });
                     });

                });
                }
          });
      });