SpiffySaxMan SpiffySaxMan - 4 months ago 37
Javascript Question

node.js SyntaxError: Unexpected token !=

I am writing an API using stormpath and node.js.
However, when I attempt to run the create_user script in node it gives me an error: "SyntaxError: Unexpected token !="

this is my tools.js file:



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





This is my create_user.js file:



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





when I run node create_user.js this is the response:

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)


Please help!

I have had people suggest I simply remove this.createClient ={...) and this.createUser ={...), however when I remove those I simply get this in return: "[Function]"

Answer

I think there are several problems with the code

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

In here you seem to be redeclaring this.createClient inside createClient. I would change it to look like below

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


}

};

You have a similar error in the createUser function so I changed that too. I don't know if that will solve all your problems given that I do not have strompath to run on my machine, but those are the ones I could find. Hope that helps