1

I'm trying to change findTitleLatestRev function to lambda in node.js. This is use in a mongoose to define the method of a schema. Before:

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

call it at:

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});
    });
};

Before changing, it does work well. I change it to:

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

That cause an error:

`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

)`

3
  • Where is find defined? Can you paste the whole class? If find is a mongoose method then it won´t work. Commented May 12, 2017 at 7:14
  • 'findTitleLatestRev' function is used in a mongoose module to define a method of schema. 'find' is the the method in mongoose or mongoDB Commented May 12, 2017 at 7:20
  • Yeah. However when using lambda the 'this' will be the class where you have defined that static and not the mongoose schema. If you want to use lambda you should change 'this' for something like this.revisionModel.find(... Commented May 12, 2017 at 7:22

1 Answer 1

3

Arrow functions treat this differently from older function definitions.

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.

Basically, arrow functions will always have the this value of the context where it was defined. It cannot be rebound to another context.

The class assumes that this will be set by the function in the conventional manner, thus it does not work for the arrow function.

If you have access to the class, you could rewrite it to handle how this is being used to support arrow functions. Otherwise, I believe it'd be much simpler to just continue using conventional functions for that.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.