Node.js syntax error "unexpected token" on "exports."
Asked Answered
V

1

6

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)

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)
Vouge answered 12/1, 2013 at 22:5 Comment(4)
Usually you just need to do exports.funcName = function() { }, so I'd start there. If I could see the rest of the code around where it's failing I could probably be more helpful.Purdy
If you are referring to "module.exports" instead of "exports". I have tried the latter too with same error.Vouge
This could be a really silly mistake as I am a newbie. Do we need to "require" something to use "exports"?Vouge
again, use exports.userLogin = instead of module.exports.userLogin. Secondly is there any other code in the file? 3rd, how are you getting that error, what command are you using?Purdy
P
12

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

Purdy answered 12/1, 2013 at 22:17 Comment(1)
Could you explicitly show the different part? Or make it bold? it's very hard to read through linek seeking for the part which is different in two examplesJillianjillie

© 2022 - 2024 — McMap. All rights reserved.