For Workers & Pages, what is the name of the domain?
https://api.aajneeti.workers.dev/
What is the error message?
Error: TypeError: NetworkError when attempting to fetch resource.
What is the issue or error you’re encountering
worker fails why I try to insert data into the cloudflaer D1
What steps have you taken to resolve the issue?
googled a lot
What are the steps to reproduce the issue?
my worker had no issues but when I added the code to insert a row in the table, it stops working.
what’s the right way to debug it?
is this how we should pass the env variable?
export default {
async fetch(request, env, ctx) {
try {
return await handle(request, env);
} catch (e) {
// Return exception stack trace for debugging purposes.
return new Response(e.stack, {
status: 500,
statusText: “Internal Server Error”,
});
}
},
};
function generateOTP() {
}
function divideString(str) {
}
function mergeArrays(oddArray, evenArray) {
}
function processString(input) {
}
function generateToken(mobile, pageURL, OTP, epochTime, secretKey) {
}
function extractDataFromToken(token) {
}
async function saveToDB(data, table, fields, env) {
const info = await env.DB
.prepare(‘INSERT INTO leads (page_url, project_name, name, mobile, city, ip_location) VALUES (?1, ?2,?3, ?4, ?5)’)
.bind(data.page_url, data.project_name, data.name, data.mobile, data.city, data.ip_location)
.run();
console.log(info)
}
async function handle(request, env) {
if (request.method === “OPTIONS”) {
return handleOptions(request);
} else if (request.method === “POST”) {
return handlePost(request, env);
} else if (request.method === “GET” || request.method === “HEAD”) {
// Pass-through to origin
return fetch(request);
} else {
return new Response(null, {
status: 405,
statusText: “Method Not Allowed”,
});
}
}
const corsHeaders = {
“Access-Control-Allow-Origin”: “*”,
“Access-Control-Allow-Methods”: “GET, HEAD, POST, OPTIONS”,
“Access-Control-Allow-Headers”: “Content-Type”,
};
function handleOptions(request) {
if (
request.headers.get(“Origin”) !== null &&
request.headers.get(“Access-Control-Request-Method”) !== null &&
request.headers.get(“Access-Control-Request-Headers”) !== null
) {
// Handle CORS pre-flight request
return new Response(null, {
headers: corsHeaders
});
} else {
// Handle standard OPTIONS request
return new Response(null, {
headers: {
Allow: “GET, HEAD, POST, OPTIONS”,
},
});
}
}
async function handlePost(request, env) {
if (request.headers.get(“Content-Type”) !== “application/json”) {
return new Response(null, {
status: 415,
statusText: “Unsupported Media Type”,
headers: corsHeaders,
});
}
// Detect parse failures by setting json
to null.
let json = await request.json().catch(() => null);
if (json === null) {
return new Response(“JSON parse failure”, {
status: 400,
statusText: “Bad Request”,
headers: corsHeaders,
});
}
if(json.action == ‘/lead/save’) {
// saveToDB(json, ‘table’, ‘fields’, env);
// code stops working if I uncomment the above line and call saveToDB
}
else if (json.action == ‘/lead/generateOTP’) {
}
else if (json.action == ‘/lead/verifyOTP’) {
}
else {
return new Response(“Invalid Parameters”, {
status: 400,
statusText: “Bad Request”,
headers: corsHeaders,
});
}
return new Response(JSON.stringify(json), {
headers: {
“Content-Type”: “application/json”,
…corsHeaders,
},
});
}