2017-05-12 45 views
1

我試圖在node.js中將findTitleLatestRev函數改爲lambda。 這用於貓鼬來定義模式的方法。 前:在node.js上使用lambda不起作用

RevisionSchema.statics.findTitleLatestRev = function(title, callback){ 
    return this.find({'title':title}) 
     .sort({'timestamp':-1}) 
     .limit(1) 
     .exec(callback); 
}; 

調用它在:

module.exports.getLatest=function(req,res){ 
    let title = req.query.title; 
    Revision.findTitleLatestRev(title, (err,result)=>{ 
     if (err) console.log('Cannot find ' + title + "'s latest revision!"); 
     console.log(result); 
     revision = result[0]; 
     res.render('revision.pug',{title: title, revision:revision}); 
    }); 
}; 

改變之前,它的工作很好。我將其更改爲:

`RevisionSchema.statics.findTitleLatestRev = (title, callback)=> 
    {this.find({'title':title}) 
     .sort({'timestamp':-1}) 
     .limit(1). 
     exec(callback)};` 

導致的錯誤:

`TypeError: this.find is not a function 
    at Function.RevisionSchema.statics.findTitleLatestRev (/home/tung/Documents/node/nodejs-labs/app/models/revision.js:25:8) 
    at module.exports.getLatest (/home/tung/Documents/node/nodejs-labs/app/controllers/revision.server.controller.js:24:14) 
    at Layer.handle [as handle_request] (/home/tung/Documents/node/nodejs-labs/node_modules/express/lib/router/layer.js:95:5) 
    at next (/home/tung/Documents/node/nodejs-labs/node_modules/express/lib/router/route.js:137:13) 
    at Route.dispatch (/home/tung/Documents/node/nodejs-labs/node_modules/express/lib/router/route.js:112:3) 
    at Layer.handle [as handle_request] (/home/tung/Documents/node/nodejs-labs/node_modules/express/lib/router/layer.js:95:5) 
    at /home/tung/Documents/node/nodejs-labs/node_modules/express/lib/router/index.js:281:22 
    at Function.process_params (/home/tung/Documents/node/nodejs-labs/node_modules/express/lib/router/index.js:335:12) 
    at next (/home/tung/Documents/node/nodejs-labs/node_modules/express/lib/router/index.js:275:10) 
    at Function.handle (/home/tung/Documents/node/nodejs-labs/node_modules/express/lib/router/index.js:174:3) 
    at router (/home/tung/Documents/node/nodejs-labs/node_modules/express/lib/router/index.js:47:12) 
    at Layer.handle [as handle_request] (/home/tung/Documents/node/nodejs-labs/node_modules/express/lib/router/layer.js:95:5) 
    at trim_prefix (/home/tung/Documents/node/nodejs-labs/node_modules/express/lib/router/index.js:317:13) 
    at /home/tung/Documents/node/nodejs-labs/node_modules/express/lib/router/index.js:284:7 
    at Function.process_params (/home/tung/Documents/node/nodejs-labs/node_modules/express/lib/router/index.js:335:12) 
    at next (/home/tung/Documents/node/nodejs-labs/node_modules/express/lib/router/index.js:275:10 

)`

+0

find在哪裏定義?你能粘貼全班嗎?如果find是貓鼬方法,那麼它就不會工作。 – kimy82

+0

'findTitleLatestRev'函數用於mongoose模塊中以定義模式方法。 '發現'是在貓鼬或mongoDB的方法 –

+0

是的。但是,在使用lambda時,'this'將是您定義靜態而不是mongoose模式的類。如果你想使用lambda,你應該改變'this',像_this.revisionModel.find(..._ – kimy82

回答

3

箭頭功能從舊的函數定義治療this不同。

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/Arrow_functions

Until arrow functions, every new function defined its own this value [...]. An arrow function does not create its own this context, so this has its original meaning from the enclosing context.

基本上,箭頭功能將總是有它被定義的上下文的this值。它不能被反彈到另一個環境。

該類假定this將由函數以傳統方式設置,因此它不適用於箭頭函數。

如果您有權訪問課程,您可以重寫它以處理如何使用this來支持箭頭功能。否則,我相信只要繼續使用傳統的功能就簡單多了。