I’m working on my firs NodeJS page and have set up to just read and write a string of data when I push a button.
astra.js
const axios = require("axios");
const ASTRA_DB_ID = process.env.ASTRA_DB_ID;
const ASTRA_REGION = process.env.ASTRA_REGION;
const ASTRA_TOKEN = process.env.ASTRA_APP_TOKEN;
const COLLECTION = "test";
const BASE_URL = `https://${ASTRA_DB_ID}-${ASTRA_REGION}.apps.astra.datastax.com/api/rest/v2/keyspaces/default_keyspace/collections/${COLLECTION}`
const HEADERS = {
"x-cassandra-token": ASTRA_TOKEN,
"Content-Type": "application/json",
};
exports.handler = async function (event) {
if (event.httpMethod === "POST") {
const {_id, message} = JSON.parse(event.body);
const res = await axios.put(`${BASE_URL}/${_id}`, {_id: _id, message}, {headers:HEADERS});
return {
statusCode:200,
body: JSON.stringify({success:true, res:res.data}),
};
} else if (event.httpMethod === "GET") {
const _id = event.queryStringParameters.id;
const res = await axios.get(`${BASE_URL}/${_id}`, {headers:HEADERS});
return {
statusCode:200,
body:JSON.stringify(res.data),
};
}
return {statusCode:405, body: "Method Not Allowed"};
}
index.html
<!DOCTYPE html>
<html>
<head>
<title>DB TEST</title>
</head>
<body>
<h1>Testing</h1>
<button id="write-btn">Write to DB</button>
<button id="read-btn">Read from DB</button>
<script>
document.getElementById("write-btn").addEventListener("click", write);
document.getElementById("read-btn").addEventListener("click", read);
async function write() {
alert("test");
try {
await fetch('/api/astra', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({_id:'demo', message: 'Hellow from HTML!'})
});
alert("Message saved!");
} catch (e) {
alert(e)
}
}
async function read() {
const res = await fetch('/api/astra?id=demo');
const data = await res.json();
alert("Message: " + JSON.stringify(data));
}
</script>
</body>
</html>
netlify.toml
[build]
publish = "public"
functions = "functions"
[[redirects]]
from = "/api/*"
to = "/.netlify/functions/:splat"
status = 200
ASTRA_APP_TOKEN,ASTRA_REGION, and ASTRA_DB_ID are all configured correctly in Netlify environment variables to match the DB. However, whenever I click one of the 2 buttons, it returns:
Writing
(9)[ "AxiosError: Request failed with status code 400", " at settle (/var/task/node_modules/axios/dist/node/axios.cjs:2049:12)", " at IncomingMessage.handleStreamEnd (/var/task/node_modules/axios/dist/node/axios.cjs:3166:11)", " at IncomingMessage.emit (node:events:530:35)", " at endReadableNT (node:internal/streams/readable:1698:12)", " at process.processTicksAndRejections (node:internal/process/task_queues:90:21)", " at Axios.request (/var/task/node_modules/axios/dist/node/axios.cjs:4276:41)", " at process.processTicksAndRejections (node:internal/process/task_queues:105:5)", " at async exports.handler (/var/task/functions/astra.js:14:21)" ]
Reading
(9)[ "AxiosError: Request failed with status code 400", " at settle (/var/task/node_modules/axios/dist/node/axios.cjs:2049:12)", " at IncomingMessage.handleStreamEnd (/var/task/node_modules/axios/dist/node/axios.cjs:3166:11)", " at IncomingMessage.emit (node:events:530:35)", " at endReadableNT (node:internal/streams/readable:1698:12)", " at process.processTicksAndRejections (node:internal/process/task_queues:90:21)", " at Axios.request (/var/task/node_modules/axios/dist/node/axios.cjs:4276:41)", " at process.processTicksAndRejections (node:internal/process/task_queues:105:5)", " at async exports.handler (/var/task/functions/astra.js:21:21)" ]
Both the same error with the exception of returning different lines at which the error occurred, since, of course, reading and writing happens at different lines. Inside the DB I have a default_keyspace labeled as test. The requests are sent, but error when they try to actually fetch/put the data. Anybody know how to fix this?