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)
Ronan Boiteau
  • 9,608
  • 6
  • 34
  • 56
Saumya Tripathi
  • 71
  • 1
  • 1
  • 5
  • 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. – Jamund Ferguson Jan 12 '13 at 22:07
  • If you are referring to "module.exports" instead of "exports". I have tried the latter too with same error. – Saumya Tripathi Jan 12 '13 at 22:12
  • This could be a really silly mistake as I am a newbie. Do we need to "require" something to use "exports"? – Saumya Tripathi Jan 12 '13 at 22:13
  • 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? – Jamund Ferguson Jan 12 '13 at 22:15

1 Answers1

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

Nathan Walker
  • 182
  • 1
  • 7
Jamund Ferguson
  • 16,721
  • 3
  • 42
  • 50
  • 5
    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 examples – Vitalii Lebediev Jan 31 '14 at 12:06