How Can I Fix an Unexpected Token Code Error in My API Call Script?

This topic was automatically generated from Slack. You can find the original thread here.

this is code you helped me to build, I get “Code Error
Unexpected token (95:8)” can you help me fix it?

import axios from “axios”;

export default defineComponent({
props: {
Instance: {
type: “string”,
label: “Greem-Api device Instance Number”,

},
Green_token: {
  type: "string",
  label: "Green-Api Device Token",

},
students_path: {
  type: "string",
  label: "Students array step path",
},
content: {
  type: "string",
  label: "Whatsapp Content Path",
  },
   content_name: {
  type: "string",
  label: "Whatsapp Content Name Path",
  },
  whatsapp: {
  type: "string",
  label: "Whatsapp Number Colum Name",
  },
   base_uuid: {
  type: "string",
  label: "Base Uuid",
  },
   base_token: {
  type: "string",
  label: "Base Token",
  },
    logs_table_name: {
  type: "string",
  label: "Logs Table Name",
  **default**: "לוג שליחות",
  },
  message_type: {
  type: "string",
  label: "השדה המציין את סוג ההודעה לשליחה",
  **default**: "SendMessage",
  },
   file_url: {
  type: "string",
  label: "כתובת הקובץ לשליחה",

  },
   file_name: {
  type: "string",
  label: "שם הקובץ לשליחה כולל סיומת",

  },

},

async run({ steps }) {
const apiUrl = https://api.green-api.com/waInstance${**this**.Instance}/${**this**.message_type}/${**this**.Green_token}/;
const students = this.students_path;
const seatableApiUrl = https://cloud.seatable.io/dtable-server/api/v1/dtables/${**this**.base_uuid}/rows/;

//const students = steps.get_activ_students.$return_value;
// const apiUrl = ‘https://api.green-api.com/waInstance1101796351/SendMessage/af6a35aea200435aa8c83d09acd9f5e117cf832eb15d4b2094/’;

   **for** (**let** i = 0; i < students.length; i++) {

const chatId = ${students[i][**this**.whatsapp]}@c.us;
const message = this.content.replace(///g, ‘’);
console.log(“מספר:”, students[i][this.whatsapp]);

try {
if (this.message_type === “SendMessage”) {
await axios.post(apiUrl, {
chatId,
message
});
} else if (this.message_type === “SendFileByUrl”) {
await axios.post(apiUrl, {
chatId,
urlFile: this.file_url,
fileName: this.file_name,
caption: message
});
}
} catch (error) {
console.error(error);
}
}

    console.log(`API call ${i+1}/${students.length} made successfully.`);
  } **catch** (error) {
    console.error(`Error making API call ${i+1}/${students.length}:`, error);
  }
}

},

  _/**  // Save the response to the SeaTable table "לוג שליחות"_
    _await axios(this, {_
      _method: "POST",_
      _url: seatableApiUrl,_
      _headers: {_
        _Authorization: `Token ${this.base_token}`,_
                _},_
     _data:   {_
              _row: {'תשובה': response.data, 'מספר': chatId, 'שם תוכן': message_name},_
              _table_name: 'לוג שליחות'_
                    _},_
       
                       _});_

  _}**/_

});

It seems like there are several issues with your code. Here are a few things that stand out: 1. The import statement is incorrect. In Pipedream, you should import axios from the @pipedream/platform package, not directly from axios. So, replace import axios from "axios"; with import { axios } from "@pipedream/platform";. 2. The apiUrl and seatableApiUrl are not correctly formatted. You should use template literals (backticks `) instead of single or double quotes for string interpolation. For example, your apiUrl should be like this: const apiUrl = `https://api.green-api.com/waInstance${this.Instance}/${this.message_type}/${this.Green_token}/`; 3. There are HTML entities in your code (like <), which should be replaced with their corresponding characters (like <). 4. There are two catch blocks in your code, which seems to be a mistake. You should have one try block followed by one catch block. 5. The for loop is not correctly closed. Make sure to close the loop with a closing brace }. 6. There is a commented block of code at the end of your component. If you don’t need it, I recommend removing it to avoid confusion. Please revise your code considering these points and try again. If you still encounter issues, please provide more specific details about the error.

OK
can you fix it now? error code: Missing catch or finally clause (80:2)

import axios from “axios”;

export default defineComponent({
props: {
Instance: {
type: “string”,
label: “Greem-Api device Instance Number”,

},
Green_token: {
  type: "string",
  label: "Green-Api Device Token",

},
students_path: {
  type: "string",
  label: "Students array step path",
},
content: {
  type: "string",
  label: "Whatsapp Content Path",
  },
   content_name: {
  type: "string",
  label: "Whatsapp Content Name Path",
  },
  whatsapp: {
  type: "string",
  label: "Whatsapp Number Colum Name",
  },
   base_uuid: {
  type: "string",
  label: "Base Uuid",
  },
   base_token: {
  type: "string",
  label: "Base Token",
  },
    logs_table_name: {
  type: "string",
  label: "Logs Table Name",
  **default**: "לוג שליחות",
  },
  message_type: {
  type: "string",
  label: "השדה המציין את סוג ההודעה לשליחה",
  **default**: "SendMessage",
  },
   file_url: {
  type: "string",
  label: "כתובת הקובץ לשליחה",

  },
   file_name: {
  type: "string",
  label: "שם הקובץ לשליחה כולל סיומת",

  },

},

async run({ steps }) {
const apiUrl = https://api.green-api.com/waInstance${**this**.Instance}/${**this**.message_type}/${**this**.Green_token}/;
const students = this.students_path;
const seatableApiUrl = https://cloud.seatable.io/dtable-server/api/v1/dtables/${**this**.base_uuid}/rows/;

//const students = steps.get_activ_students.$return_value;
// const apiUrl = ‘https://api.green-api.com/waInstance1101796351/SendMessage/af6a35aea200435aa8c83d09acd9f5e117cf832eb15d4b2094/’;

   **for** (**let** i = 0; i < students.length; i++) {

const chatId = ${students[i][**this**.whatsapp]}@c.us;
const message = this.content.replace(///g, ‘’);
console.log(“מספר:”, students[i][this.whatsapp]);

    **for** (**let** i = 0; i < students.length; i++) {

const chatId = ${students[i][**this**.whatsapp]}@c.us;
const message = this.content.replace(///g, ‘’);
console.log(“מספר:”, students[i][this.whatsapp]);

try {
if (this.message_type === “SendMessage”) {
await axios.post(apiUrl, {
chatId,
message
});
} else if (this.message_type === “SendFileByUrl”) {
await axios.post(apiUrl, {
chatId,
urlFile: this.file_url,
fileName: this.file_name,
caption: message
});
}
}

    console.log(`API call ${i+1}/${students.length} made successfully.`);
  } **catch** (error) {
    console.error(`Error making API call ${i+1}/${students.length}:`, error);
  }

}
},

  _/**  // Save the response to the SeaTable table "לוג שליחות"_
    _await axios(this, {_
      _method: "POST",_
      _url: seatableApiUrl,_
      _headers: {_
        _Authorization: `Token ${this.base_token}`,_
                _},_
     _data:   {_
              _row: {'תשובה': response.data, 'מספר': chatId, 'שם תוכן': message_name},_
              _table_name: 'לוג שליחות'_
                    _},_
       
                       _});_

  _}**/_

});

OK
can you fix it now? error code: Missing catch or finally clause (80:2)

import axios from “axios”;

export default defineComponent({
props: {
Instance: {
type: “string”,
label: “Greem-Api device Instance Number”,

},
Green_token: {
  type: "string",
  label: "Green-Api Device Token",

},
students_path: {
  type: "string",
  label: "Students array step path",
},
content: {
  type: "string",
  label: "Whatsapp Content Path",
  },
   content_name: {
  type: "string",
  label: "Whatsapp Content Name Path",
  },
  whatsapp: {
  type: "string",
  label: "Whatsapp Number Colum Name",
  },
   base_uuid: {
  type: "string",
  label: "Base Uuid",
  },
   base_token: {
  type: "string",
  label: "Base Token",
  },
    logs_table_name: {
  type: "string",
  label: "Logs Table Name",
  **default**: "לוג שליחות",
  },
  message_type: {
  type: "string",
  label: "השדה המציין את סוג ההודעה לשליחה",
  **default**: "SendMessage",
  },
   file_url: {
  type: "string",
  label: "כתובת הקובץ לשליחה",

  },
   file_name: {
  type: "string",
  label: "שם הקובץ לשליחה כולל סיומת",

  },

},

async run({ steps }) {
const apiUrl = https://api.green-api.com/waInstance${**this**.Instance}/${**this**.message_type}/${**this**.Green_token}/;
const students = this.students_path;
const seatableApiUrl = https://cloud.seatable.io/dtable-server/api/v1/dtables/${**this**.base_uuid}/rows/;

//const students = steps.get_activ_students.$return_value;
// const apiUrl = ‘https://api.green-api.com/waInstance1101796351/SendMessage/af6a35aea200435aa8c83d09acd9f5e117cf832eb15d4b2094/’;

   **for** (**let** i = 0; i < students.length; i++) {

const chatId = ${students[i][**this**.whatsapp]}@c.us;
const message = this.content.replace(///g, ‘’);
console.log(“מספר:”, students[i][this.whatsapp]);

    **for** (**let** i = 0; i < students.length; i++) {

const chatId = ${students[i][**this**.whatsapp]}@c.us;
const message = this.content.replace(///g, ‘’);
console.log(“מספר:”, students[i][this.whatsapp]);

try {
if (this.message_type === “SendMessage”) {
await axios.post(apiUrl, {
chatId,
message
});
} else if (this.message_type === “SendFileByUrl”) {
await axios.post(apiUrl, {
chatId,
urlFile: this.file_url,
fileName: this.file_name,
caption: message
});
}
}

    console.log(`API call ${i+1}/${students.length} made successfully.`);
  } **catch** (error) {
    console.error(`Error making API call ${i+1}/${students.length}:`, error);
  }

}
},

  _/**  // Save the response to the SeaTable table "לוג שליחות"_
    _await axios(this, {_
      _method: "POST",_
      _url: seatableApiUrl,_
      _headers: {_
        _Authorization: `Token ${this.base_token}`,_
                _},_
     _data:   {_
              _row: {'תשובה': response.data, 'מספר': chatId, 'שם תוכן': message_name},_
              _table_name: 'לוג שליחות'_
                    _},_
      
                       _});_

  _}**/_

});

It seems like you’re missing a catch or finally clause in your code, which is causing the error. In JavaScript, a try block must be followed by either a catch or finally block. Here’s your code with the missing catch clause added: import axios from "axios"; export default defineComponent({ props: { // Your props here... }, async run({ steps }) { const apiUrl = `https://api.green-api.com/waInstance${this.Instance}/${this.message_type}/${this.Green_token}/`; const students = this.students_path; const seatableApiUrl = `https://cloud.seatable.io/dtable-server/api/v1/dtables/${this.base_uuid}/rows/`; for (let i = 0; i < students.length; i++) { const chatId = `${students[i][this.whatsapp]}@c.us`; const message = this.content.replace(/\//g, ''); console.log("מספר:", students[i][this.whatsapp]); try { if (this.message_type === "SendMessage") { await [axios.post](http://axios.post)(apiUrl, { chatId, message }); } else if (this.message_type === "SendFileByUrl") { await [axios.post](http://axios.post)(apiUrl, { chatId, urlFile: this.file_url, fileName: this.file_name, caption: message }); } console.log(`API call ${i+1}/${students.length} made successfully.`); } catch (error) { console.error(`Error making API call ${i+1}/${students.length}:`, error); } } }, }); This code will now catch any errors that occur during the execution of the try block and log them to the console. If you want to handle the errors differently, you can modify the code inside the catch block.