Friday, 12 May 2017

Using lambda at node.js does't work

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

)`



via Tung Reeboo

No comments:

Post a Comment