Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Code Project Community,

Good day. I'm a beginner in Javascript and would like to seek help on resolving the issue I get with this code. I receive below error message when I try to capture a text in a page and send it to my Slack Channel using a Webhook URL.

Error in Console in the Page:
Expected declaration but found ‘/’. Skipped to next declaration.

Thank you very much in advance.

What I have tried:

This is the code:

const site = document.querySelector(".plain-text-display").textContent.split('\n').forEach(line => {if(line.includes("test")) {console.log(line.split(", ")[0])}});

I tried using single quote but I still have the same issue, value of the variable is not being captured to slack channel.

const site = document.querySelector('.plain-text-display').textContent.split('\n').forEach(line => {if(line.includes('site')) {console.log(line.split(', ')[0])}});

However, if is execute this command in Console of the page, it works. I get the value.

document.querySelector('.plain-text-display').textContent.split('\n').forEach(line => {if(line.includes('site')) {console.log(line.split(', ')[0])}});
Posted
Updated 15-Jun-23 3:01am
v6
Comments
Richard Deeming 5-Apr-23 4:16am    
Are you sure that the error is referring to this line? There's nothing in that line which would cause the error you mentioned, so check the preceding lines for problems.

Also, which browser are you using? If you're unlucky enough to be stuck on Internet Explorer, then arrow functions won't work.
Richard Deeming 6-Apr-23 3:24am    
One obvious thing that jumps out: you're telling the server that the body is application/x-www-form-urlencoded, but you're sending JSON data instead.

There's also no point using template strings to set a variable to the same value as another (eg: const data = `${ticketurl}`;); you could simply set the variable directly (const data = ticketurl;).
Richard Deeming 6-Apr-23 3:27am    
Also note that your site variable will be undefined, since forEach doesn't return a value.

Depending on your requirements, you probably want a combination of filter and map:
const site = document.querySelector('.plain-text-display').textContent.split('\n').filter(line => line.includes('test')).map(line => line.split(', ')[0]);

1 solution

As discussed in the comments, forEach does not return anything. A combination of filter and map is probably what's required:
JavaScript
const site = document.querySelector('.plain-text-display').textContent.split('\n').filter(line => line.includes('test')).map(line => line.split(', ')[0]);
Array.prototype.filter() - JavaScript | MDN[^]
Array.prototype.map() - JavaScript | MDN[^]

Also, sending a content type of application/x-www-form-urlencoded with a JSON body probably won't work, and will simply confuse the server.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900