node.js SyntaxError:意外的标记!=
我正在使用stormpath和node.js编写一个API。 但是,当我尝试在节点中运行create_user脚本时,它给了我一个错误:“SyntaxError:Unexpected token!=”
这是我的tools.js文件:
var stormpath = require('stormpath') var util = require ('util'); var appHref = 'https://api.stormpath.com/v1/applications/KzUPEioLybUfu2YwUjb8o'; var client; var app; // Find the user's home directory (works on both Windows and *nix): var home = process.env[(process.platform === 'win32' ? 'USERPROFILE' : 'HOME')]; var apiKeyFilePath = home + '/.stormpath/apiKey.properties'; module.exports = { createClient: function (apiKeyFilePath) { this.createClient ={ if (apiKeyFilePath != 'undefined') { stormpath.loadApiKey(apiKeyFilePath, function apiKeyFileLoaded(err, apiKey) { client = new stormpath.Client({ apiKey: apiKey }); console.log('apiKey found... Client Created'); }); } else { var apiKey = new stormpath.ApiKey( process.env['STORMPATH_CLIENT_APIKEY_ID'], process.env['STORMPATH_CLIENT_APIKEY_SECRET'] ); var client = new stormpath.Client({ apiKey: apiKey }); console.log('apiKey created... Client Created'); } } }, createUser: function () { this.createUser={ this.createClient; client.getDirectory(usersHref, callback); var account = { username: 'example', email: 'example@gmail.com', password: 'Changeme!' }; RMFapp.createAccount(account, function(err, createdAccount) { console.log(createdAccount); }); } } };
这是我的create_user.js文件:
var stormpath = require('stormpath') var util = require ('util'); var tools = require('./tools'); var appHref = 'https://api.stormpath.com/v1/application/KzUPEioLybUfu2YwUjb8o'; var usersHref = 'https://api.stormpath.com/v1/directories/13tj6f50q7jJ7WvDu6SxHa'; var client; var app; console.log(tools.createUser);
当我运行节点create_user.js这是响应:
if (apiKeyFilePath != 'undefined') { SyntaxError: Unexpected token != at exports.runInThisContext (vm.js:53:16) at Module._compile (module.js:373:25) at Object.Module._extensions..js (module.js:416:10) at Module.load (module.js:343:32) at Function.Module._load (module.js:300:12) at Module.require (module.js:353:17) at require (internal/module.js:12:17) at Object.<anonymous> (/home/spiffysaxman/projects/RMFapp/RMF/www/assets/api/v1/create_user.js:3:13) at Module._compile (module.js:409:26) at Object.Module._extensions..js (module.js:416:10)
请帮忙!
我有人build议我干脆删除this.createClient = {…)和this.createUser = {…),但是当我删除那些我只是得到这个回报:“[function]”
我认为这个代码有几个问题
1)从createClient开始
createClient: function (apiKeyFilePath) { this.createClient ={ if (apiKeyFilePath != 'undefined') { stormpath.loadApiKey(apiKeyFilePath, function apiKeyFileLoaded(err, apiKey) { client = new stormpath.Client({ apiKey: apiKey }); console.log('apiKey found... Client Created'); }); }
在这里你似乎在createClient里面重新声明this.createClient。 我会改变它看起来像下面
module.exports = { createClient : function(){ if (apiKeyFilePath != 'undefined') { stormpath.loadApiKey(apiKeyFilePath, function apiKeyFileLoaded(err, apiKey) { client = new stormpath.Client({ apiKey: apiKey }); console.log('apiKey found... Client Created'); }); } else { var apiKey = new stormpath.ApiKey( process.env['STORMPATH_CLIENT_APIKEY_ID'], process.env['STORMPATH_CLIENT_APIKEY_SECRET'] ); var client = new stormpath.Client({ apiKey: apiKey }); console.log('apiKey created... Client Created'); } } , createUser: function () { this.createClient; client.getDirectory(usersHref, callback); var account = { username: 'example', email: 'example@gmail.com', password: 'Changeme!' }; RMFapp.createAccount(account, function(err, createdAccount) { console.log(createdAccount); }); } };
你在createUser函数中有类似的错误,所以我也改变了。 我不知道这是否会解决所有的问题,因为我没有strompath在我的机器上运行,但那些是我能find的。 希望有所帮助
在tools.js
:
createClient: function (apiKeyFilePath) { this.createClient ={ // this line is the issue, remove it and the } that closes it if (apiKeyFilePath != 'undefined') {
而且,一旦这个问题得到解决,你将会得到另一个错误,因为:
createUser: function () { this.createUser={ // this line this.createClient; // and this line client.getDirectory(usersHref, callback);
这将需要以相同的方式纠正。