Wajid Ahmad
Wajid Ahmad

Reputation: 31

SyntaxError: Unexpected token =

Trying to run this code:

const { Model } = require('objection');
const Userposts = require("./Userposts.js");

class User extends Model{
    
    
    static tableName = "users";

    static relationMappings = {

    userposts: {
      relation: Model.HasManyRelation,
      modelClass: Userposts,
      join: {
        from: 'users.id',
        to: 'userposts.user_id'
      }
    }
  }
}

module.exports = User;

Keep getting this error msg:

 static tableName = "users";
                 ^

SyntaxError: Unexpected token =
    at Module._compile (internal/modules/cjs/loader.js:723:23)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)

Anyone knows why i cant get my code to work in an online editor like AWS Cloud9? This code works perfectly fine in VScode.

Upvotes: 1

Views: 837

Answers (1)

Abed Murrar
Abed Murrar

Reputation: 101

try assigning the static variable as a function instead of a variable like

class User extends Model {

  static get tableName() {
    return "users";
  }

  static get relationMappings() {

    return {
      relation: Model.HasManyRelation,
      modelClass: Userposts,
      join: {
        from: "users.id",
        to: "userposts.user_id",
      },
    };
    
  }

}

Upvotes: 1

Related Questions