简体   繁体   English

从模块函数返回的对象未定义

[英]Returned object undefined from module function

This has to be a scope issue that I'm not familiar with. 这一定是我不熟悉的范围问题。 I have a small module I've written as so: 我有一个这样写的小模块:

(function () {

    var getPlanInfo = function (id, conn) {
        conn.query('SELECT * FROM `items` WHERE `id` = ?', [id], function (error, result) {
            if (error) console.error('Query error: ' + error.stack);

            console.log(result[0]); // Everything is great

            return result[0];
        });
    };

    modules.exports.getPlanInfo = function (id, conn) { return getPlanInfo(id, conn); // Typo }

})();

Here comes the problem. 问题来了。 When I call it from anywhere (inside the module itself or another file), the return value is always undefined. 当我从任何地方(在模块本身或另一个文件内部)调用它时,返回值始终是不确定的。 I checked from within the function, the query returns the result as expected. 我从函数中进行了检查,查询按预期返回了结果。

var backend = require('./module.js');
var t = backend.getPlanInfo();

t is undefined . t是undefined This is the same if I call that method from inside the module itself (another function within that module). 如果我从模块本身(该模块内的另一个函数)内部调用该方法,则是相同的。

I'm familiar with the callback principle in javascript and how objects and functions have to be passed around as an argument to remain in scope. 我熟悉javascript中的回调原理,以及如何将对象和函数作为自变量传递来保持作用域。 Is this the issue here or is this a node.js particularity? 这是这里的问题还是node.js的特殊性?

I tried in in the Developer Console (Chrome), works as expected. 我在开发人员控制台(Chrome)中尝试过,可以正常工作。

conn.query() looks like it is async. conn.query()看起来像是异步的。 Thus, you can't return its result from getPlanInfo() because getPlanInfo() returns long before the result is available. 因此,您无法从getPlanInfo()返回其结果,因为getPlanInfo()返回的时间早于结果可用。 Returning result[0] from the conn.query() callback just returns a piece of data back into the conn.query() infrastructure. conn.query()回调返回result[0]只是将一部分数据返回到conn.query()基础结构中。 getPlanInfo() has long before already returned. getPlanInfo()早已返回。

If you want an async result, then you will have to change getPlanInfo() to use a mechanism that supports getting async results such as a direct callback or a promise or something like that. 如果想要异步结果,则必须更改getPlanInfo()以使用支持获取异步结果的机制,例如直接回调或promise之类。

Here's a plain callback way: 这是一种简单的回调方式:

var getPlanInfo = function (id, conn, callback) {
    conn.query('SELECT * FROM `items` WHERE `id` = ?', [id], function (error, result) {
        if (error) {
            console.error('Query error: ' + error.stack);
            callback(error);
            return;
        }

        console.log(result[0]); // Everything is great

        callback(0, result[0]);
    });
};

modules.exports.getPlanInfo = getPlanInfo;

Then, the caller of that module would look like this: 然后,该模块的调用者将如下所示:

var m = require('whatever');
m.getPlanInfo(id, conn, function(err, result) {
    if (err) {
        // error here
    } else {
        // process result here
    }
});

You don't return anything from getPlanInfo . 您不会从getPlanInfo返回任何getPlanInfo Probably you wanted to write modules.exports.getPlanInfo = function (id, conn) { return getPlanInfo; } 可能您想编写modules.exports.getPlanInfo = function (id, conn) { return getPlanInfo; } modules.exports.getPlanInfo = function (id, conn) { return getPlanInfo; }

(with return getPlanInfo; instead of return getPlanInfo(); ) (使用return getPlanInfo;而不是return getPlanInfo();

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

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