I have a todo list where I want to store the todo tasks in a database under todo. But I get error. I have tried PUT and POST but same error. Why do I get this error? And how can I send data from input field to store in the database?
main.js
const form = document.getElementById('form')
const input = document.getElementById('input')
const todosUL = document.getElementById('todos')
const todos = JSON.parse(localStorage.getItem('todos'))
if(todos){
todos.forEach(todo => addTodo(todo))
}
async function addTodoToFirebase() {
try {
const baseURL = 'https://todoparon-default-rtdb.europe-west1.firebasedatabase.app/'
const url = baseURL + `todo.json`
const response = await fetch(url)
let data = await response.json()
console.log(data)
// Send the updated data back to Firebase
const updateResponse = await fetch(url, {
method: 'PUT',
body: input,
headers: {
'Content-type': 'application/json; charset=UTF-8',
},
});
if (updateResponse.ok) {
console.log('Data updated successfully in Firebase!');
} else {
console.error('Failed to update data in Firebase.');
}
} catch (error) {
console.error('Error while updating data:', error);
}
}
form.addEventListener('submit', (e) => {
e.preventDefault()
addTodo()
addTodoToFirebase()
})