0

Добрый вечер. Как преобразовать ответ с бд(mongodb) в структуру dom. Запрос приходит но на страничке видна одна запись. А в коллекции их несколько. Вот код.

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");

1 ответ 1

1

pravelno bydet tak

done: function (err, window) {
    var $ = window.$;
    collection.mapReduce(mapreduce.map, mapreduce.reduce, {
        out: {
            inline: 1
        },
        verbose: true
    }, function (err, rezult, stats) {
        if (!err) {
            //console.log(rezult);
            for (var i = 0; i < rezult.length; i++) {
                $(".list-group").append($("<a/>", {
                    href: "#",
                    class: "list-group-item"
                }).append($("<h4/>", {
                    class: "list-group-item-heading"
                }).text(rezult[i]['_id'])).append($("<p/>", {
                    class: "list-group-item-text"
                }).text(rezult[i]['value'])));
            };
            res.write(window.document.innerHTML, "utf8");
            res.end();
        } else {
            console.log('err mapReduce');
        };
    });
}

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.