Need help with http-functions.js - POST

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!

The insert() is attempting to insert the all of the fields of the entire JSON, and the collection does not have all of those fields. The error that you received, sort of points that out, " field name prefixed with symbol ‘$’ ". The insert() function is trying to insert the fields $version and $etag .

The object passed to the insert() function must be exactly what needs to be inserted, You will need to extract the fields that you want from the JSON and create the appropriate object to user for the insert() . Refer to the insert() API for details.

Thanks. So basically I need to deconstruct that JSON and build a new one that has exactly only the fields that I need which matches the Collection?

Any references to sample code that can help? Thanks again!

You want to do something like this:

let toInsert = {
  "FirstName":   body['FirstName'],
  "LastName":    body['LastName']
};

You might have to make adjustments, but that’s the basic idea.

Perfect. I had to parse the JSON and create an object, but it worked.

  var obj = JSON.parse(body) 
      let toInsert = {
      "firstName":   ['FirstName'],
      "lastName":    ['LastName']
	}

Thanks!