“导出”上的 Node.js 语法错误“意外令牌”。

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14298409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:56:12  来源:igfitidea点击:

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

node.jsexpressfb-graph

提问by Saumya Tripathi

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.

我一直在努力学习 node.js。我正在尝试创建一个简单的 node.js web api 和一个 html-javascript 前端来使用 Facebook auth 登录并将 Facebook id 存储在 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

编辑#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

编辑#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.

这就是我在用户路由中的全部内容。实际上,我犯了一个非常愚蠢的错误,我在前面留下了逗号 graph = require('fbgraph')而不是分号。修复此语法错误后,我收到此错误。

}
^
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)

回答by Jamund Ferguson

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.userLogininstead of module.exports.userLogin

也使用exports.userLogin代替module.exports.userLogin