'end' 在 Iron Router for Meteor 中 this.response 未定义
'end' undefined for this.response in Iron Router for Meteor
我已经使用 Iron Router 为我的 Meteor 服务器创建了一个 HTTP POST 端点。我想用 JSON 状态和一些其他元数据将响应发送回请求者。
这是端点的代码:
Router.route('/new_video', {where: 'server'})
.post(function(){
var body = this.request.body;
this.response.setHeader('Content-Type', 'application/json');
var filename = body.filename;
console.log('New video uploaded for: ' + filename);
Meteor.call('newUpload', filename, function(error, results){
if (error){
throw new Meteor.Error("new-video-upload-failed", "New video could not be uploaded.");
var message = {
url: '/new_video',
status: 'success'
};
}
else{
var videoId = results;
console.log('Returned video id: ' + videoId);
var message = {
url: '/new_video',
status: 'failure'
};
}
this.response.end(JSON.stringify(message));
});
});
Meteor 控制台正在打印:
=> Meteor server restarted
I20151002-15:51:26.311(-4)? New recording for: 1422776235,43.46756387,-80.54130886.mp4
I20151002-15:51:26.515(-4)? Returned video id: QiHXxZSb2sn9aNRPs
I20151002-15:51:26.569(-4)? Exception in delivering result of invoking 'newRecording': TypeError: Cannot call method 'end' of undefined
I20151002-15:51:26.569(-4)? at shared/routes.js:79:17
由于在Meteor.call
.
中引入了另一个函数回调而修改了this
的值,这是JS的一个常见陷阱
如果您使用的是带有 ES2015 箭头函数的 Meteor 1.2,您可以改用此函数声明语法来解决问题:
Meteor.call('newUpload', filename, (error, results) => {
// here 'this' will keep referencing the POST route context
// so you can safely use this.response
});
如果您不使用 Meteor 1.2,请改用此语法:
Meteor.call('newUpload', filename, function(error, results) {
// inner function is bound to parent function 'this'
}.bind(this));
我已经使用 Iron Router 为我的 Meteor 服务器创建了一个 HTTP POST 端点。我想用 JSON 状态和一些其他元数据将响应发送回请求者。
这是端点的代码:
Router.route('/new_video', {where: 'server'})
.post(function(){
var body = this.request.body;
this.response.setHeader('Content-Type', 'application/json');
var filename = body.filename;
console.log('New video uploaded for: ' + filename);
Meteor.call('newUpload', filename, function(error, results){
if (error){
throw new Meteor.Error("new-video-upload-failed", "New video could not be uploaded.");
var message = {
url: '/new_video',
status: 'success'
};
}
else{
var videoId = results;
console.log('Returned video id: ' + videoId);
var message = {
url: '/new_video',
status: 'failure'
};
}
this.response.end(JSON.stringify(message));
});
});
Meteor 控制台正在打印:
=> Meteor server restarted
I20151002-15:51:26.311(-4)? New recording for: 1422776235,43.46756387,-80.54130886.mp4
I20151002-15:51:26.515(-4)? Returned video id: QiHXxZSb2sn9aNRPs
I20151002-15:51:26.569(-4)? Exception in delivering result of invoking 'newRecording': TypeError: Cannot call method 'end' of undefined
I20151002-15:51:26.569(-4)? at shared/routes.js:79:17
由于在Meteor.call
.
this
的值,这是JS的一个常见陷阱
如果您使用的是带有 ES2015 箭头函数的 Meteor 1.2,您可以改用此函数声明语法来解决问题:
Meteor.call('newUpload', filename, (error, results) => {
// here 'this' will keep referencing the POST route context
// so you can safely use this.response
});
如果您不使用 Meteor 1.2,请改用此语法:
Meteor.call('newUpload', filename, function(error, results) {
// inner function is bound to parent function 'this'
}.bind(this));