Hey there I need to submit a google form using an API, there are three fields in google form name, email & profile, which are identified as "entry.575833505", "entry.1168209987" & "entry.1027774066" respectively. The code is as follows
in backend "backend.jsw"
export function getcertificate() { let endPoint = "https://docs.google.com/forms/u/0/d/e/1FAIpQLSemY58g-nMoFe9KfetmJLGGYnTeMbfQJgyhR9RDhNr5iEI30A/formResponse" let name = "Test User Name" let email = "testuser@email.com" let profile = "testuser" return fetch(endPoint, { 'method': 'post', 'headers': { 'Content-Type': 'application/x-www-form-urlencoded' }, 'body': JSON.stringify({"entry.575833505":name,"entry.1168209987":email,"entry.1027774066":profile}) }) .then((httpResponse) => { if (httpResponse.ok) { console.log("inside if part of then") return httpResponse.json(); } else { console.log("inside else part of then") return Promise.reject('Fetch did not succeed'); } }) .then((json) => { console.log("inside then") return json.result; }) .catch(err => console.log(`error: ${err}`)); }
Calling the module from the front end with the click of a button
export async function getcert_click(event) { var resp = await getcertificate() console.log(`response: ${resp}`) }
In the console, I see this error "response: undefined", even I tested API in Postman and it was submitting values. Please help and let me know what I am doing wrong. Thanks in advance!
For starters you are not handling the returned error from the fetch. the .catch does not return anything. Secondly i believe the response is actually a html response so a text response not a JSON response. you are trying to get the response as JSON. Your better off checking that the status in the httpResponse is a 200 OK to decide if it went through
export function getcertificate() { let endPoint = "https://docs.google.com/forms/u/0/d/e/1FAIpQLSemY58g-nMoFe9KfetmJLGGYnTeMbfQJgyhR9RDhNr5iEI30A/formResponse"; let name = "Test User Name"; let email = "testuser@email.com"; let profile = "testuser"; return fetch(endPoint, { method: "post", headers: { "Content-Type": "application/x-www-form-urlencoded", }, body: JSON.stringify({ "entry.575833505": name, "entry.1168209987": email, "entry.1027774066": profile, }), }) .then((httpResponse) => { if (httpResponse.ok) { return { success: true }; } else { return Promise.reject("Fetch did not succeed"); } }) .catch((err) => { return { success: false, error: `Error with submission: ${String(err)}` }; }); } export async function getcert_click(event) { var resp = await getcertificate(); if (resp.success) { console.log('Submission successful, status 200 OK') } else { console.error('Submission failed; ', resp.error) } }