Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/371.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.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实例方法:对象#<;对象>;没有方法';HandlerRequest&x27;_Javascript_Node.js_Express - Fatal编程技术网

找不到javascript实例方法:对象#<;对象>;没有方法';HandlerRequest&x27;

找不到javascript实例方法:对象#<;对象>;没有方法';HandlerRequest&x27;,javascript,node.js,express,Javascript,Node.js,Express,以下代码起作用: Experimenters = function(db) { Object.apply(this, arguments); this.db = db; }; util.inherits(Experimenters, Object); Experimenters.prototype.getAll = function(req, res) { return this.db.Experimenter.getAllExperimentersWithE

以下代码起作用:

Experimenters = function(db)
{
    Object.apply(this, arguments);
    this.db = db;
};
util.inherits(Experimenters, Object);


Experimenters.prototype.getAll = function(req, res)
{    
    return this.db.Experimenter.getAllExperimentersWithExperiments()
    .then(function(exptrs) {
        res.json(200, exptrs);
    })
    .catch(function(error) {
        res.json(500, error);
    });
};
但是,我的每个请求处理程序的代码几乎都是相同的,因此我想通过创建一个通用请求处理程序来减少重复代码:

Experimenters.prototype.handleRequest = function(promise, res)
{
    return promise
    .then(function(success){
        res.json(200, success);
    })
    .catch(function(error) {
        if (error instanceof dbErrors.NotFoundError) {
            res.json(404, error);
        } else if ((error instanceof dbErrors.ValidationError) ||
                   (error instanceof dbErrors.UniqueConstraintError)) {
            res.json(422, error);
        } else {
            // unknown error
            console.error(error);
            res.json(500, error);
        }
    });
};
并修改我的请求处理程序,如下所示:

Experimenters.prototype.getAll = function(req, res)
{
    this.handleRequest(
        this.db.Experimenter.getAllExperimentersWithExperiments(),
        res);
};
但我得到了:

TypeError: Object #<Object> has no method 'handleRequest' 
从我的app.js调用:

 //edited for brevity
 var config = require(path.join(__dirname, "..", "config")),
     createDB = require(path.join(__dirname, "models")),
     routes = require(path.join(__dirname, "routes"));

 var db = createDB(config);
 app.set("db", db);
 // edited for brevity
 routes.initialize(app);
更新:

出现此错误是因为您应该将
exptrs
绑定到如下函数:

app.get("/api/experimenters", exptrs.getAll.bind(exptrs));
这是因为您正在将函数
exptrs.getAll
作为参数传递到
app
调用的
get
函数中,因此
exptrs中的
将引用
app

因此,另一种解决方案是将匿名函数传递给
get

app.get("/api/experimenters", function(){exptrs.getAll()});

通常,当您遇到诸如
对象#没有“handleRequest”方法之类的错误时
, 要么意味着

  • .prototype.handleRequest()
    未正确定义,或

  • 调用
    .handleRequest()
    的对象实际上不是 正确的对象

  • 我相信
    .handleRequest
    的返回应该是
    promise().then(…).catch(…)
    ,而不是
    promise.then(…).catch(…)
    ,因为只有
    promise
    而没有
    你没有调用函数

    近似

    var b = function(){
     return 1   
    };
    
    function a(c){
        return c
    }
    
    var d = a(b);
    
    console.log(d);
    
    //it will not log 1, unless 
    //function a(c){
    //  return c()
    //}
    

    .getAll
    中,您也应该返回
    this.handleRequest(..)
    ,而不仅仅是调用它。

    我可以通过以下方式解决此问题:

    首先,我注意到我的handleRequest()没有使用“this”,因此它可能是一个简单的函数

    其次,我修改了handleRequest(),在Promise周围添加了一个Promise.try()

    handleRequest = function(promise, res)
    {
        return Promise.try(function(){
            return promise;
        })
        .then(function(success){
            res.json(200, success);
        })
        .catch(function(error) {
            if (error instanceof dbErrors.NotFoundError) {
                res.json(404, error);
            } else if ((error instanceof dbErrors.ValidationError) ||
                       (error instanceof dbErrors.UniqueConstraintError)) {
                res.json(422, error);
            } else {
                // unknown error
                console.error(error);
                res.json(500, error);
            }
        });
    };
    

    getAll()
    函数中的
    this
    的值取决于调用
    getAll()
    函数的方式,而不是它的定义方式,并且调用函数的代码似乎在上述代码中缺失?可能与第一个脚本的第3行中的,我想在返回声明之后不会运行任何内容。@FelixKling谢谢。是的,这和bind()有关,所以对于exptrs.getAll.bind(这个)“this”指的是exptrs?@ckot很抱歉我犯了一个粗心的错误。
    exptrs.getAll.bind(this)
    中的
    this
    是指
    module.exports.initialize
    中的
    this
    ,这样做
    exptrs.getAll.bind(this)
    将使
    中的
    this
    成为
    模块.exports
    。您需要做的是绑定(exptrs),以便
    exptrs
    将成为
    exptrs.getAll
    中的
    this
    。它仍然有效,但我添加了exptrs.getAll.bind(exptrs),并且它没有破坏任何东西。对于这个简单的请求处理程序,可能没有必要这样做,但是对我所有的处理程序(可能调用其他实例方法)这样做应该可以确保工作,对吗?@ckot是的,除非您希望函数中的
    this
    引用其他内容(在本例中为
    app
    )。要了解有关
    bind
    的更多信息,您可以查看此项。是的,这是因为您正在将函数
    exptrs.getAll
    作为参数传递到
    中。get
    函数由
    app
    调用,而
    exptrs中的
    函数将引用
    app
    。这很有意义,谢谢!
    handleRequest = function(promise, res)
    {
        return Promise.try(function(){
            return promise;
        })
        .then(function(success){
            res.json(200, success);
        })
        .catch(function(error) {
            if (error instanceof dbErrors.NotFoundError) {
                res.json(404, error);
            } else if ((error instanceof dbErrors.ValidationError) ||
                       (error instanceof dbErrors.UniqueConstraintError)) {
                res.json(422, error);
            } else {
                // unknown error
                console.error(error);
                res.json(500, error);
            }
        });
    };