2016-07-22 89 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