js给出了意外的标识符

时间:2019-03-22 07:34:55

标签: javascript node.js app42

当我尝试从cmd行执行js文件时,出现此错误。我不确定是否可以提供这样的钥匙。

const App42 = require('./js/App42-all-1.6.min');
const mod = require('./js/buddy');

module.exports = {

var API_KEY = '014883a05a4902889c860272b3c4*******568072cf82cfc31a42c165f0f8cc6';
var SEC_KEY = 'b3b2df57b86a7fdabe66a96a*******7e04fbd95c6ddef942a3c844878eafbf05';


App42.initialize(API_KEY,SEC_KEY); 

hello: function() {
  return "Hello";
}
}

我遇到的错误是:

LB.js:6
    var APIKEY = '014883a05a4902889c860272b3c2170ae145*****2cf82cfc31a42c165f0f8cc6';
        ^^^^^^

SyntaxError: Unexpected identifier
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:617:28)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
at Function.Module._load (module.js:498:3)
at Function.Module.runMain (module.js:694:10)
at startup (bootstrap_node.js:204:16)
at bootstrap_node.js:625:3

在放弃投票之前,可以有人说解决方案。这不是您对尝试学习js的新用户的反应。

2 个答案:

答案 0 :(得分:2)

您正在导出对象,但是对象语法无效:

module.exports = {
//Shouldn't be using var here as it's just a property of an object
var API_KEY = '014883a05a4902889c860272b3c4*******568072cf82cfc31a42c165f0f8cc6';
var SEC_KEY = 'b3b2df57b86a7fdabe66a96a*******7e04fbd95c6ddef942a3c844878eafbf05';

//Shouldn't be calling a function here as it's also in the object definition
App42.initialize(API_KEY,SEC_KEY); 

hello: function() {
  return "Hello";
}
}

您应该只导出对象的键和值。 因此,根据您要实际“导出”的内容,您可以尝试

API_KEY:'014883a05a4902889c860272b3c4*******568072cf82cfc31a42c165f0f8cc6',
SEC_KEY:'b3b2df57b86a7fdabe66a96a*******7e04fbd95c6ddef942a3c844878eafbf05',
App42.initialize(API_KEY,SEC_KEY); 
module.exports = {

 API_KEY:API_KEY,
 SEC_KEY:SEC_KEY,
 App42: App42,



hello: function() {
  return "Hello";
}
}

然后require这个文件时,您可以访问各种属性和功能,例如

const foo = require('./myfile');
console.log(foo.App42);
console.log(foo.hello());
console.log(foo.API_KEY);

您可以阅读有关对象here

的更多信息

答案 1 :(得分:0)

这里的问题是,您直接在对象声明(module.exports)内部声明变量并运行函数,这就是为什么它不编译。

您应该执行以下操作:

const App42 = require('./js/App42-all-1.6.min');
const mod = require('./js/buddy');

const API_KEY = '014883a05a4902889c860272b3c4*******568072cf82cfc31a42c165f0f8cc6';
const SEC_KEY = 'b3b2df57b86a7fdabe66a96a*******7e04fbd95c6ddef942a3c844878eafbf05';
App42.initialize(API_KEY,SEC_KEY); 

module.exports = {
  hello: function() {
    return "Hello";
  }
}