Node.js syntax error "unexpected token" on "exports."


Question

I have been trying to learn node.js. I am trying to create a simple node.js web api and a html-javascript front end to login using Facebook auth and store Facebook id in Mongodb.

I was able to do it by following tutorials available online.

Now I want to segregate the code into multiple files but when I try to create a route "user" and expose functions via exports. I am getting the following error.

module.exports.userLogin = function(req,res){
      ^ SyntaxError: Unexpected token .
    at Module._compile (module.js:437:25)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:362:17)
    at require (module.js:378:17)
    at Object.<anonymous> (C:\Users\Saumya\Desktop\vhsharedraft\web.js:2:6)
    at Module._compile (module.js:449:26)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:356:32)

Thanks in advance.

EDIT#1

module.exports.userLogin = function(req,res){
    graph.setAccessToken(req.session.fb.access_token);

    graph.get("/me", function(err, data) {
        if(err){
            console.log('Error obtaining data.');
            return;
        }
        console.log(data);
    }

}

EDIT # 2

var mongo = require('mongodb'),
graph = require('fbgraph');



exports.userLogin = function(req,res){
    graph.setAccessToken(req.session.fb.access_token);

    graph.get("/me", function(err, data) {
        if(err){
            console.log('Error obtaining data.');
            return;
        }
        console.log(data);
    }
}

This is all I have in user route. Actually, I was making a real silly mistake, I left a comma in front of graph = require('fbgraph') instead of semi colon. After fixing this syntax error, I am getting this error.

}
^
SyntaxError: Unexpected token }
    at Module._compile (module.js:437:25)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:362:17)
    at require (module.js:378:17)
    at Object.<anonymous> (C:\Users\Saumya\Desktop\vhsharedraft\web.js:2:6)
    at Module._compile (module.js:449:26)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:356:32)

Thank you for prompt replies.

1
6
2/7/2014 2:17:21 PM

Accepted Answer

You have a typo here:

graph.get("/me", function(err, data) {
    if(err){
        console.log('Error obtaining data.');
        return;
    }
    console.log(data);
}

It should be:

graph.get("/me", function(err, data) {
    if(err){
        console.log('Error obtaining data.');
        return;
    }
    console.log(data);
}); /* Note the added parenthesis here */

Also use exports.userLogin instead of module.exports.userLogin

11
2/18/2014 4:46:35 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon