2017-05-12 1 views
1

변경하려고합니다. findTitleLatestRev은 node.js에서 람다로 작동합니다. mongoose에서 스키마의 메서드를 정의하는 데 사용됩니다. 하기 전에 :node.js에서 람다를 사용하면 작동하지 않습니다.

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

어디에서 찾을 수 있습니까? 전체 수업을 붙여 줄 수 있습니까? 찾기가 몽구스 방법이라면 그것은 효과가 없습니다. – kimy82

+0

'findTitleLatestRev'함수는 몽구스 모듈에서 스키마 메소드를 정의하는 데 사용됩니다. 'find'는 mongoose 또는 mongoDB의 메소드입니다. –

+0

예. 그러나 람다를 사용할 때 'this'는 정적 인 스키마를 정의한 클래스가 될 것입니다. 람다를 사용하고 싶다면 'this'를 _this.revisionModel.find (... – kimy82

답변

3

화살표 기능은 이전의 함수 정의 다르게 this을 취급합니다.

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.

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

기본적 화살표 함수는 항상 정의 된 콘텍스트의 this 값을 가질 것이다. 다른 컨텍스트로 리바운드 될 수 없습니다.

클래스는 일반적인 방식으로 this이 함수에 의해 설정된다고 가정하므로 화살표 함수에서는 작동하지 않습니다.

클래스에 액세스 할 수있는 경우 화살표 기능 지원에 this이 사용되는 방식을 처리하도록 클래스를 다시 작성할 수 있습니다. 그렇지 않으면, 나는 그것을 위해 전통적인 기능을 사용하는 것이 훨씬 더 간단 할 것이라고 믿는다.

관련 문제