Hello.
I’m trying to populate a Wix collection using Wix http-function post() method.
I’ve already created an empty collection with field names matching the json I want to post.
I’ve created a backend file called http-function.js with the following code:
import {created, serverError} from 'wix-http-functions';
import wixData from 'wix-data';
export function post_addProperty(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("Properties", body);
} )
.then( (results) => {
options.body = {
"inserted": results
};
return created(options);
} )
// something went wrong
.catch( (error) => {
options.body = {
"error": error
};
return serverError(options);
} );
}
I’m using a Java Jersey web-service to call the Wix API.
At the moment, however, I’m trying to get it working with Chrome Postman first.
When I send the request I get a 500 internal serve error:
{“error”:{“code”:“WD_PERMISSION_DENIED”}}
Ok, I found the solution.
I had to edit collection permission and select ‘Anyone’ where is asked who can create content for this collection. Pretty simple, but maybe a note here wix-http-functions - Velo API Reference - Wix.com could be useful. I wasted few hours for that.
For anyone interested, I also added more code to the previous snippet:
// get the request body
return request.body.json()
.then( (body) => {
// insert the item in a collection
let recordInsert = {
"propertyId": body.propertyID,
"property_marker_title": body.title,
"property_marker_snippet": body.snippet,
//etc.etc.
}
return wixData.insert("Properties", recordInsert);
} )
It worked both from Chrome Postman and from my Java Jersey web-service.
I wonder if anyone manage to get this to work. I am looking for ways to insert the data into my sandbox collections. Is there a way to do this? or the only way to use wix-functions is to reference the live collection only.
You’re getting the square bracket because the Data Collection property name is not being recognized for the field; the square bracket is a new field that’s been added. Somewhere in your function and in the tooling setup sending to your http-function, there’s a name mismatch. The field name in your Data Collection is different from the name in your .js file.