简体   繁体   English

NodeJS模块中的函数返回返回undefined,但它不是真的

[英]Function return in NodeJS module returns undefined and it's not true

I have a mongo.js module in my NodeJS application in which I try to check if document exists, if not, save to database, if yes, return error to the routes.js file which called the module function. 我在我的NodeJS应用程序中有一个mongo.js模块,我尝试检查文档是否存在,如果没有,保存到数据库,如果是,则将错误返回到称为模块函数的routes.js文件。

Here's my module: 这是我的模块:

var appRoot = require('app-root-path'),
config = require(appRoot + '/config/config'),
db = require('mongojs').connect(config.db.host);

module.exports = {
    saveSpammer: function(ip, userAgent, fn) {
        var exist = this.find('spammers', {ip:ip, userAgent:userAgent});

        console.log(exist); // returns undefined

        if (!exist) {
            return this.save('spammers', {ip: ip, userAgent: userAgent, date: (new Date())}, fn);
        } else {
            return fn('You have already submitted a request!');
        }
    },

    save: function(col, data, fn) {
        var col = this.getCol(col);

        col.insert(data, {w:1}, function(err, records) {
            if (err) {
                return fn(err);
            } else {
                return fn(null, records);
            }
        });
    },

    find: function(col, query, fn) {
        var col = this.getCol(col);

        col.find(query, function(err, docs) {
            console.log(docs); // returns list of documents from mongo
            console.log(docs ? 1 : 0); // returns 1

            if (docs) {
                return true;
            } else {
                return false;
            }
        });
    }
}

When I output "docs" variable in find functions, i see correct data, then based on it i return true or false based on the existence of a document, the problem is that return from a function find to function saveSpammer gives me "undefined". 当我在查找函数中输出“docs”变量时,我看到正确的数据,然后基于它我根据文档的存在返回true或false,问题是从函数find返回函数saveSpammer给我“undefined” 。

I am using a regular function return as in JavaScript, maybe I am missing something in the concept of NodeJS? 我在JavaScript中使用常规函数返回,也许我在NodeJS的概念中缺少一些东西?

Please assist 请协助

The problem in your example is that the col.find() call executes asynchronously. 您的示例中的问题是col.find()调用异步执行。

So 所以

var exist = this.find('spammers', {ip:ip, userAgent:userAgent});
console.log(exist); // returns undefined

is the expected behavior since this.find does not return anything. this.find没有返回任何内容的预期行为。 You can see that there is no return in the definition of the find function. 您可以看到find函数的定义中没有return

You need to use the callback mechanism 您需要使用回调机制

this.find('spammers', {ip:ip, userAgent:userAgent}, function(err, exist) {
    console.log(exist);
    .. rest of the code here
});

and modify the find function with 并使用修改find功能

if (err) {
    fn(err);
} else if (docs) {
    fn(null, true);
} else {
    fn(null, false);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM