Need to find out why my code in the backend is not working. I heard that if you get a 500 then that is a server-side issue. Please see my server-side code below and the code file name used. My GET function on the server-side is working correctly when I need to use it but for some reason, my POST function is not cooperating. Could someone please help me out with this one it would really make a difference on how to solve this issue?
Code File Image
The site is published and I am putting in the correct format for my site which is the following:
https://user.wixsite.com/mysite/_functions/country/
Backend Code (http-functions.js - server-side)
// URL looks like:
// https://www.mysite.com/_functions/myFunction/
// or:
// https://user.wixsite.com/mysite/_functions/country/
export function post_country(request) {
let options = {
"headers": {
"Content-Type": "application/json"
}
};
// get the request body
return request.body.json()
.then( (body) => {
// insert the item in a collection
return wixData.insert("countries", body);
} )
.then( (results) => {
options.body = {
"inserted": results
};
return created(options);
} )
// something went wrong
.catch( (error) => {
options.body = {
"error": error
};
return serverError(options);
} );
}
The following is how I am calling the fetch method in another Wix editor to post information.
Page Code:
import {postCountry,getMCountry} from 'backend/test';
postCountry().then(function (res) {
console.log("ran post")
console.log(res);
})
Backend Code:
import wixFetch from 'wix-fetch';
export async function postCountry() {
const url = "https://ninollc.wixsite.com/blogtut/_functions/country";
//let user = wixUsers.currentUser.id;
const bodyd = JSON.stringify({
"countryName": "recipe",
"city": "rating"
});
return await wixFetch.fetch(url, {
method: 'post',
headers: {
'Content-Type': 'application/json'
// 'Content-Type': 'application/x-www-form-urlencoded',
},
body: bodyd
}).then((response) => {
console.log(response);
if (response.ok) {
return response.json();
}
return Promise.reject('fetch to wix function has failed ' + response.status + '\n' + response.statusText);
}).catch(e => {
console.log(e);
});
}
Any help or guidance is welcomed.
Thanks!
Nino