找不到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);
}
});
};