Good evening. How to convert a response from a database (mongodb) into a dom structure. The request arrives but one entry is visible on the page. And there are several of them in the collection. Here is the code.
eventEmitter.on("collectionUserChat",function(callback){ async.connectDB(function (db) { async.getCollection("userchat", db, function (collectionUserChat) { async.find(collectionUserChat,function(rezult){ async.toArray(rezult,function(data){ data.forEach(function(i){ callback(i); }); }); }) }); }); }); async.connectDB(function (db) { async.getCollection("userstest", db, function (collectionUserTest) { async.find(collectionUserTest,function(rezult){ async.toArray(rezult,function(data){ data.forEach(function(i){ console.log(i); eventEmitter.emit("collectionUserChat",function (u){ if(i["pass"] == pass){ res.render("chat", function (err, data) { jsdom.env({ html: data, scripts: ["http://code.jquery.com/jquery-1.9.1.min.js"], done: function (err, window) { var $ = window.$; $(".list-group").append($("<a/>", { href: "#", class: "list-group-item" }).append($("<h4/>", { class: "list-group-item-heading" }).text(u)).append($("<p/>", { class: "list-group-item-text" }))); res.write(window.document.innerHTML, "utf8"); res.end(); } }); }); }else{ res.render("error",function(err,data){ jsdom.env({ html: data, scripts: ["http://code.jquery.com/jquery-1.9.1.min.js"], done: function (err, window) { var $ = window.$; res.write(window.document.innerHTML, "utf8"); res.end(); } }); }); } }) }); }); }) }); }); async.js
var mongo = require("mongodb"), client = mongo.MongoClient; function connectDB(callback){ client.connect("mongodb://******************", function (err, db) { if(err){ console.log("failed connect"); } callback(db); }); } function getCollection(name,db,callback){ db.collection(name, function (err, collection) { if(err){ console.log("missing data"); } callback(collection); }); } function find(collection,callback){ collection.find(function(err,rezult){ if(err){ console.log("can not find field"); } callback(rezult); }); } function toArray(rezult,callback){ rezult.toArray(function(err,data){ if(err){ console.log("err array"); } callback(data); }); } function forEach(data,callback){ data.forEach(function(i){ callback(i); }); } function closeDB(db){ db.close(function(err,rezult){ if(err){ console.log("can not close connect"); }else{ console.log(rezult); } }); } exports.connectDB = connectDB; exports.getCollection = getCollection; exports.find = find; exports.toArray = toArray; exports.forEach = forEach; exports.closeDB = closeDB; console.log("async has require");