fetch not working

fetch is not working in my program, going to attempt to show it as well as possible. the initial call :
async chat(message) {
console.log(message);
let payload = {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer " + TWITCH_APP_TOKEN,
"Client-Id": TWITCH_APP_ID
},
body: {
"broadcaster_user_id": "12341234",
"sender_id": "12341234",
"message": message,
"for_source_only": "true"
}
};
let url = "http://api.twitch.tv/helix/chat/messages";
let response = await fetch(url, payload);

if (!response.ok) {
return Response.json({
status: response.status,
error: 'oauth error?'
}, {
status: 500
});
}
if (!response.headers?.get('content-type')?.includes('application/json')) {
return Response.json({
status: response.status,
error: 'malformed json'
}, {
status: 500
});
}
const json = await response.json();
console.log(json);
return
}
async chat(message) {
console.log(message);
let payload = {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": "Bearer " + TWITCH_APP_TOKEN,
"Client-Id": TWITCH_APP_ID
},
body: {
"broadcaster_user_id": "12341234",
"sender_id": "12341234",
"message": message,
"for_source_only": "true"
}
};
let url = "http://api.twitch.tv/helix/chat/messages";
let response = await fetch(url, payload);

if (!response.ok) {
return Response.json({
status: response.status,
error: 'oauth error?'
}, {
status: 500
});
}
if (!response.headers?.get('content-type')?.includes('application/json')) {
return Response.json({
status: response.status,
error: 'malformed json'
}, {
status: 500
});
}
const json = await response.json();
console.log(json);
return
}
the method that calls that method:
handler(message) {
if (message === undefined) return;
// for (const heart in #hearts) if (message.startsWith(heart)) {
// this.#commands.announce(message.substring(1).trim(), hearts[heart]);
// return;
// }
this.chat(message);
};
handler(message) {
if (message === undefined) return;
// for (const heart in #hearts) if (message.startsWith(heart)) {
// this.#commands.announce(message.substring(1).trim(), hearts[heart]);
// return;
// }
this.chat(message);
};
basically the logic set up is like this:
export default{
async fetch(request, env, ctx){
// constants declared, blah blah
class Database{} // uses async fetch here too
class TwitchChat{} // this is the class that contains those methods
switch(request.method){} // switch on the method, with switch on pathnames as well within
}
}
export default{
async fetch(request, env, ctx){
// constants declared, blah blah
class Database{} // uses async fetch here too
class TwitchChat{} // this is the class that contains those methods
switch(request.method){} // switch on the method, with switch on pathnames as well within
}
}
@James here is more of the logic, i copy pasted the code you used and that didn't seem to work so not sure what the issue could be
5 Replies
James
James6mo ago
On initial glance, this.chat is async, but you're not await-ing it. So if you're calling it, but not awaiting, and then something else in the chain kills the response by returning, your code in this.chat might never complete.
Senti3ntB3ing
Senti3ntB3ingOP6mo ago
hope this fixes it
James
James6mo ago
If you can provide a minimal reproduction in full that I can copy/paste and run to replicate the issue, I can take a closer look, but I would guess your issue lies somewhere in how this is being called, as the request alone works fine as I illustrated at https://canary.discord.com/channels/595317990191398933/779390076219686943/1385639027998195753
Senti3ntB3ing
Senti3ntB3ingOP6mo ago
it looks like its kinda working now, im getting a response back, can't read the response yet but i have it now yup i got it working now! thanks for catching the non awaited call, ive been migrating so much code from one thing to another so many little things have changed and its hard to catch them all
James
James6mo ago
No worries, glad you got it figured out!

Did you find this page helpful?