I am very new to Velo coding. Any help is greatly appreciated. I am trying to take JSON generated by an external site (Cognito Forms) and insert it into my Wix Collection. I have created a simple collection “Test” with only 3 fields: id (PK), firstName and lastName
When I run test the function in dev mode, I always get this error and the insert does not happen.
Data inserted contains field name prefixed with symbol ‘$’. It is not recommended to use such field names as it will not work with some data operations.
Here is an example of the JSON that is generated by Cognito:
{
"Form": {
"Id": "11",
"InternalName": "Test",
"Name": "Test"
},
"$version": 7,
"$etag": "W/\"datetime'2021-05-12T23%3A16%3A35.3271654Z'\"",
"Entry": {
"CustomerCard": null,
"DateCreated": "2021-05-12T23:16:35.286Z",
"DateSubmitted": "2021-05-12T23:16:35.208Z",
"DateUpdated": "2021-05-12T23:16:35.286Z",
"IsBeta": false,
"LastPageViewed": null,
"Number": 14,
"Order": null,
"Origin": {
"City": null,
"CountryCode": null,
"IpAddress": "98.160.135.70",
"IsImported": false,
"Region": null,
"Timezone": null,
"UserAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36"
},
"PaymentToken": null,
"Status": "Submitted",
"Timestamp": "2021-05-12T23:16:35.208Z",
"Version": 1,
"FirstName": "Bob",
"LastName": "Jones",
"Id": "11-14"
}
Here is my sample code:
export function post_addFNA(request) {
let options = {
"headers": {
"Content-Type": "application/json"
}
};
// get the request body
return request.body.text()
.then( (body) => {
// insert the item in a collection
return wixData.insert("Test", JSON.parse(body));
} )
.then( (results) => {
options.body = {
"inserted": results
};
return created(options);
} )
// something went wrong
.catch( (error) => {
options.body = {
"error": error
};
return serverError(options);
} );
}
Collection “Test”
It seems that the JSON being consumed is not being handled properly.
Also, I assume that the field names in the JSON must exactly match the Field Key in the collection. They do, but I wanted to check if I am missing something.
Thanks in advance for your feedback!