2016-07-22 152 views
1

我正在使用stormpath和node.js编写一个API。 然而,当我试图在节点上运行脚本create_user它给了我一个错误:“语法错误:!意外标记=”node.js SyntaxError:意外的令牌!=

这是我的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: '[email protected]', 
 
     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) 

请帮帮忙!

我有人建议我干脆删除this.createClient = {...)和this.createUser = {...),但是当我删除那些我只是得到这个回报:“[Function]”

+0

这是上面的一行:'this.createClient = {'我不认为你在这里意味着这条线(我认为你的意思只是上面的createClient函数定义 – rgthree

回答

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'); 
    }); 
    } 

在这里开始,你似乎重新声明this.createClient内createClient代码

1)。 我会改变它看起来像下面

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: '[email protected]', 
    password: 'Changeme!' 
    }; 
    RMFapp.createAccount(account, function(err, createdAccount) { 
    console.log(createdAccount); 
    }); 


} 

}; 

你在的createUser功能类似的错误,所以我改变了这一点。我不知道这是否会解决所有的问题,因为我没有strompath在我的机器上运行,但是那些是我能找到的。希望有帮助

+0

做出这些更改后,它只是返回“[函数]” – SpiffySaxMan

+0

tools.createUser应该在它后面有括号,因为它是create_user.js中的函数调用 –

+0

修复了它,谢谢! – SpiffySaxMan

0

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); 

这将需要以同样的方式予以纠正。

+0

做出这些更改后,它只是返回“[函数]” – SpiffySaxMan