我有以下关系: https://pastebin.com/hMZRJe0S
我需要给用户指定用户界面
我使用Java脚本尝试获取usuario的ID,然后是rol的ID,然后是funcion的ID,最后是ius。通过获取匹配的元素,然后将id转换为数组,以获取下一个表(或集合)的元素
fox示例,这是我获取rol的ID的方法:
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);
}
然后我得到这个“拓扑被破坏”
C:\ Users \ NORMA \ Desktop \ EXPRESS_CRASH_COURSE \ node_modules \ mongodb \ lib \ utils.js:132 犯错 ^
MongoError:拓扑已被破坏 在initializeCursor(C:\ Users \ NORMA \ Desktop \ EXPRESS_CRASH_COURSE \ node_modules \ mongodb-core \ lib \ cursor.js:596:25) 在nextFunction(C:\ Users \ NORMA \ Desktop \ EXPRESS_CRASH_COURSE \ node_modules \ mongodb-core \ lib \ cursor.js:456:12) 在Cursor.next(C:\ Users \ NORMA \ Desktop \ EXPRESS_CRASH_COURSE \ node_modules \ mongodb-core \ lib \ cursor.js:766:3) 在Cursor._next(C:\ Users \ NORMA \ Desktop \ EXPRESS_CRASH_COURSE \ node_modules \ mongodb \ lib \ cursor.js:216:36) 在fetchDocs(C:\ Users \ NORMA \ Desktop \ EXPRESS_CRASH_COURSE \ node_modules \ mongodb \ lib \ operations \ cursor_ops.js:217:12) 在toArray(C:\ Users \ NORMA \ Desktop \ EXPRESS_CRASH_COURSE \ node_modules \ mongodb \ lib \ operations \ cursor_ops.js:247:3) 在executeOperation(C:\ Users \ NORMA \ Desktop \ EXPRESS_CRASH_COURSE \ node_modules \ mongodb \ lib \ utils.js:416:24) 在Cursor.toArray(C:\ Users \ NORMA \ Desktop \ EXPRESS_CRASH_COURSE \ node_modules \ mongodb \ lib \ cursor.js:829:10) 在C:\ Users \ NORMA \ Desktop \ EXPRESS_CRASH_COURSE \ routes \ api \ members.js:75:18 结果(C:\ Users \ NORMA \ Desktop \ EXPRESS_CRASH_COURSE \ node_modules \ mongodb \ lib \ utils.js:410:17) [nodemon]应用程序崩溃-等待文件更改,然后再开始...
答案 0 :(得分:0)
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);
});
});
});
});
}
});
});