Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript NodeJs如何将变量传递到导出函数中_Javascript_Node.js_Api - Fatal编程技术网

Javascript NodeJs如何将变量传递到导出函数中

Javascript NodeJs如何将变量传递到导出函数中,javascript,node.js,api,Javascript,Node.js,Api,我有教程制作的nodejsrestfulapi。在我的端点中,我想传递导出函数。例如: // main.js REST_ROUTER.prototype.handleRoutes = function(router, connection, md5) { router.get("/create", function(req, res) { var query = ... connection.query(query,function(err, row)

我有教程制作的nodejsrestfulapi。在我的端点中,我想传递导出函数。例如:

// main.js
REST_ROUTER.prototype.handleRoutes = function(router, connection, md5) {
    router.get("/create", function(req, res) {
        var query = ... 
        connection.query(query,function(err, row) {...}
    });
}
但我想这样做:

// main.js
var example = require('./example.js');

REST_ROUTER.prototype.handleRoutes = function(router, connection, md5) {
    router.get("/create", example.create);
}

// example.js
exports.create = function(req, res) {
    var query = ... 
    connection.query(query,function(err, row) {...}
}

但是,example.js中没有我的
连接。我怎样才能通过呢?

像这样的东西可能会有帮助:

// main.js
var example = require('./example.js');

REST_ROUTER.prototype.handleRoutes = function(router, connection, md5) {
    router.get("/create", example.create(connection));
}

// example.js
exports.create = function(connection) {
    return function(req, res) {
       var query = ... 
       connection.query(query,function(err, row) {...}
    }
}

绑定示例以创建
example.create.Bind(示例,连接)


导出。连接。
。@Cerbrus您能解释一下吗?只是一个猜测。别介意我
// main.js
var example = require('./example.js');

REST_ROUTER.prototype.handleRoutes = function(router, connection, md5) {
    router.get("/create", example.create.bind(example,connections));
}

// example.js
exports.create = function(connection, req, res) {
    var query = ... 
    connection.query(query,function(err, row) {...}
}