Node.js语法错误“意外令牌"在“出口"上. [英] Node.js syntax error "unexpected token" on "exports."
问题描述
我一直在尝试学习node.js.我正在尝试创建一个简单的node.js网络api和一个html-javascript前端,以使用Facebook身份验证登录并将Facebook ID存储在Mongodb中.
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);
}
}
编辑#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);
}
}
这就是我在用户路线中所拥有的全部.
实际上,我犯了一个真正的愚蠢的错误,我在graph = require('fbgraph')
前面留了一个逗号,而不是半冒号.
修复此语法错误后,出现此错误.
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)
推荐答案
您在这里有错字:
graph.get("/me", function(err, data) {
if(err){
console.log('Error obtaining data.');
return;
}
console.log(data);
}
应该是:
graph.get("/me", function(err, data) {
if(err){
console.log('Error obtaining data.');
return;
}
console.log(data);
}); /* Note the added parenthesis here */
也可以使用exports.userLogin
代替module.exports.userLogin
这篇关于Node.js语法错误“意外令牌"在“出口"上.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!