为什么我这样传递后不能调用router.get?

时间:2016-10-19 05:02:04

标签: javascript node.js express this

我有节点/快速服务器

jQuery(document).ready(function($){
	jQuery('.fancybox-inner').append('<a href="#" class="new-img-holder"></a>');   
	jQuery('.fancybox-image').appendTo('new-img-holder');
});

我正在尝试这样做:

<div class="fancybox-outer">
  <div class="fancybox-inner" style="overflow: visible; width: 640px; height: 463px;">
    <img class="fancybox-image" src="cine-sudarshan.jpg" alt="">
  </div>
</div>

我收到此错误

//I'm using these
var express = require('express');
var router = express.Router();

Well OK,这是第509行

applyRouter = function(rget){
    rget('/something', function(req,res,next){
        console.log("anything");
    });
}
applyRouter(router.get);

path/to/express/lib/router/index.js:509 var route = this.route(path) ^ TypeError: Cannot read property 'route' of undefined 在这里引用了什么?

我试过

// create Router#VERB functions
methods.concat('all').forEach(function(method){
  proto[method] = function(path){
    var route = this.route(path)
    route[method].apply(route, slice.call(arguments, 1));
    return this;
  };
});

this

同样的错误......发生了什么事?为什么我不能这样做?

我能做到

var outerThis = this;
applyRouter = function(rget){
    console.log("inside", this);
    rget.apply(outerThis, ['/something', function(req,res,next){
        console.log("anything");
    }]);
}
applyRouter(router.get);

所以我错误地设置了console.log("outside", this); //gives {} applyRouter = function(rget){ console.log("inside", this); rget.apply({}, ['/something', function(req,res,next){ console.log("anything"); }]); } applyRouter(router.get); applyRouter = function(router){ router.get('/something', function(req,res,next){ console.log("anything"); }); } applyRouter(router); 期望this的内容是什么? router具体在哪里变更?

0 个答案:

没有答案