Reputation: 842
I have some posts into Firebase posts.json file that I have manually entered, that look like below, post1, post2...
When I am entering new data - formData object into Firebase with post request like this:
const submitHandler = e => {
e.preventDefault();
const err = validate();
if(err === false) {
setFormData(formData)
const requestOptions = {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({formData})
};
fetch('https://blog-d8b04-default-rtdb.europe-west1.firebasedatabase.app/posts.json', requestOptions)
.then(response => setLoading(true))
.then(data => setLoading(false));
}
}
I'm getting post like this, with unique keys as names that is generating firebase, and below formData. But I want they to be the same format like posts I have entered manually into firebase(posts1, post2). Is there a way to achieve this?
Upvotes: 1
Views: 2004
Reputation: 598740
A JSON literal { formData }
is actually shorthand for { formData: formData }
, which explains the format you end up with.
You're looking for:
body: JSON.stringify(formData)
So without the {}
around formData
.
Upvotes: 3