使用Node.js在MongoDB操作上进行回调 [英] Callback at MongoDB Operation With Node.js

查看:80
本文介绍了使用Node.js在MongoDB操作上进行回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于组织目的,我将源代码分为多个模块,例如,我的node.js应用程序具有 user 模块,负责从中检索用户信息MongoDB数据库。我正在做这样的事情:

For organization purposes I'm separating my source code into modules, for example I have the user module on my node.js app which is responsable for retrieving user information from a MongoDB database. I'm doing something like this:

var mongo = require("mongodb"),
    Server = mongo.Server,
    Db = mongo.Db;

var server = new Server("localhost", 27017, { auto_reconnect: true });
var db = new Db("users", server);

module.exports = {
    login: function(user, pass, callback) {
        var reg_result = null;

        db.open(function (err, db) {
            if(!err) {
                db.collection("users", function(err, collection) {
                    collection.findOne(
                        {
                            "username": user,
                            "password": pass
                        },
                        function(err, item) {
                            if(!err) {
                                reg_result = item;
                            } else {
                                reg_result = "error";
                            }
                        }
                    );
                });
            } else {
                reg_result = "error";
                console.log("ERROR: " + err);
            }
        });

        callback(reg_result);
    }
}

并在我的测试脚本上执行如下:

And executing it on my test script like this:

var user = require("./user.js");

user.log("test", "test", function(msg) {
    console.log(msg);
});

它执行数据库操作并检索值,但每次仅返回,当我不初始化 reg_result 变量时,它返回 undefined 。我应该怎么做才能纠正这个问题?

It does the database operation and retrieves the value, but every time it only returns null, when I don't initialize the reg_result variable it returns undefined. What should I do to correct this?

我在<$ c上使用 console.log 进行了调试$ c> user.js 和 item 已输出,但是我想要回调,因此可以在其他来源上使用该项目,例如我的测试脚本

I debugged using console.log on the user.js and the item was outputted, but I want to have the callback so I can use the item on other sources, like my test script

推荐答案

您正在立即调用回调函数,但是进入db需要花费时间,并且因此异步完成。而是在适当的时间使用作为最后一个参数传递给 .findOne()函数的函数的结果参数调用回调。回调应该得到一个错误参数和一个结果参数:

You are calling the callback function right away, but going to the db takes time and is therefore done asynchronously. Instead, call your callback at the appropriate time using the result argument of the function passed as the last parameter to the .findOne() function. The callback should get an error argument and a result argument:

login: function(user, pass, callback) {
    db.open(function (err, db) {
        if(!err) {
            db.collection("users", function(err, collection) {
                collection.findOne(
                    {
                        "username": user,
                        "password": pass
                    },
                    function(err, item) {
                        if(!err) {
                            callback(null,item);
                        } else {
                            callback("error");
                        }
                    }
                );
            });
        } else {
            callback("error",null);
        }
    });
}


user.login("test", "test", function(err,msg) {
    if( err ) {
      //error occured above;
    } else {
      //success
      console.log(msg);
    }
});

这只是通过mongodb-driver使用的相同模式。
希望有帮助。

this is just pulling the same pattern through that the mongodb-driver is using. hope it helps.

这篇关于使用Node.js在MongoDB操作上进行回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆