Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript : Unexpected token; 'constructor, function, accessor or variable'

I have the below class written in type script. When I compile it, it errors out saying

"src\main\MqttClientWrapper.ts(24,2): error TS1068: Unexpected token. A construct or, method, accessor, or property was expected.".

Below is the code I have.

var mqtt :any = require('mqtt');

export interface IWillMessage {
  topic: string;
  payload: string;
  qos: number;
  retain: string;
}

export interface IMessageReceivedCallBack {
  onMessageReceived(message : string);
}

export interface IMqttOptions {
  clientId: string;
  keepAlive: number;
  clean: string;
  reconnectPeriod: string;
  will: IWillMessage;
}

export default class MqttClientWrapper {

 client : any;

constructor(url: string, mqttOptions : IMqttOptions, messageReceivedCallBack : IMessageReceivedCallBack) {
   client = mqtt.connect(url, mqttOptions);
   client.on('message',function(topic : string, message : string){
     messageReceivedCallBack.onMessageReceived(message);
   }
}

subscribeMessage(topic : string) {
  client.subscribe(topic);
}

publishMessage(topic : string, message : string, level : number ) {
  client.publish(topic,message,level);
}

}

The error is pointing to the line,

 client : any;

I have tried " var client :any; " and " let client : any " as well. Still I get the same error. Also below lines of errors are found in the trace..

src\main\MqttClientWrapper.ts(26,16): error TS1005: ',' expected.
[16:44:41] [Typescript] TypeScript error: src\main\MqttClientWrapper.ts(26,16):
error TS1005: ',' expected.
src\main\MqttClientWrapper.ts(26,38): error TS1005: ',' expected.
[16:44:41] [Typescript] TypeScript error: src\main\MqttClientWrapper.ts(26,38):
error TS1005: ',' expected.
src\main\MqttClientWrapper.ts(26,78): error TS1005: ',' expected.
[16:44:41] [Typescript] TypeScript error: src\main\MqttClientWrapper.ts(26,78):
error TS1005: ',' expected.
src\main\MqttClientWrapper.ts(26,106): error TS1005: ';' expected.
[16:44:41] [Typescript] TypeScript error: src\main\MqttClientWrapper.ts(26,106):
 error TS1005: ';' expected.
src\main\MqttClientWrapper.ts(31,1): error TS1005: ',' expected.
[16:44:41] [Typescript] TypeScript error: src\main\MqttClientWrapper.ts(31,1): e
rror TS1005: ',' expected.
src\main\MqttClientWrapper.ts(33,24): error TS1005: ',' expected.
[16:44:41] [Typescript] TypeScript error: src\main\MqttClientWrapper.ts(33,24):
error TS1005: ',' expected.
src\main\MqttClientWrapper.ts(33,34): error TS1005: ';' expected.
[16:44:41] [Typescript] TypeScript error: src\main\MqttClientWrapper.ts(33,34):
error TS1005: ';' expected.
src\main\MqttClientWrapper.ts(37,22): error TS1005: ',' expected.
[16:44:41] [Typescript] TypeScript error: src\main\MqttClientWrapper.ts(37,22):
error TS1005: ',' expected.
src\main\MqttClientWrapper.ts(37,40): error TS1005: ',' expected.
[16:44:41] [Typescript] TypeScript error: src\main\MqttClientWrapper.ts(37,40):
error TS1005: ',' expected.
src\main\MqttClientWrapper.ts(37,56): error TS1005: ',' expected.
[16:44:41] [Typescript] TypeScript error: src\main\MqttClientWrapper.ts(37,56):
error TS1005: ',' expected.
src\main\MqttClientWrapper.ts(37,67): error TS1005: ';' expected.
[16:44:41] [Typescript] TypeScript error: src\main\MqttClientWrapper.ts(37,67):
error TS1005: ';' expected.
src\main\MqttClientWrapper.ts(41,1): error TS1128: Declaration or statement expe
cted.

Below is the code with the line numbers.

enter image description here

What am I doing wrong here?are those errors also there because of the first "unexpected token error" or something wrong in those lines as well? Please advice.

like image 418
AnOldSoul Avatar asked May 16 '16 11:05

AnOldSoul


People also ask

How do you fix unexpected token a constructor method accessor or property was expected?

The "Unexpected token. A constructor, method, accessor, or property was expected" error occurs when we use the var or let keywords to declare a class property or the function keyword in a class. To solve, the error remove the var , let and function keywords from your class.

How do I fix an unexpected token?

As you write your JavaScript application, the unexpected token error always occurs because JavaScript expected a specific syntax that's not fulfilled by your current code. You can generally fix the error by removing or adding a specific JavaScript language symbol to your code.


1 Answers

You can't define class members with let or var, you can only use public or private or nothing (or static).

So this is what causes your problem:

export default class MqttClientWrapper {
    let client : any; // <- right here

    // ...
}

This is only the case in the images you attached, not in the code you posted.

like image 114
Nitzan Tomer Avatar answered Sep 28 '22 07:09

Nitzan Tomer