Updating record in collection with webhook; put vs post

I have set up import of records from Google sheet to Wix collection using “http-functions.js” and Zapier.
I first used “post” to submit records successfully into the Wix collection, except that does not appear to update existing records; it only adds new rows, even when the primary field in the Google sheet (and Wix collection) is left unchanged while another field is modified.
I then changed the type of update to “put”. I changed the type in Zapier to put, and in Wix I only modified the original function to put_functionName instead of post_functionName, leaving everything else the same.
Zapier report “success”, but now only a new empty row is added in the Wix collection, when I change a secondary field in Google sheet and leave primary filed unchanged. So the put operation does not seem to be working at all.
What am I overlooking in changing from post to put?

I would want the existing record in Wix collection to be updated with the new value of the secondary field when the primary (“Title”) field is left unchanged.
Is this possible?

Thank you very much for any suggestions.

What is the code that you have in the http-functions.js? How are you adding records to the the collection?

To update a record, you should be using the update() database function. Or, you an use save() which either inserts or updates.

Thank you very much for looking into my issue(s) with webhook.

I have tried to revise the webhook–using save() instead of insert()–that successfully inserted new records, but now I am getting “bad request” error in Zapier–no doubt because I do not have the grammar correct. Below are the insert script that worked and then below that my revision using save() that is giving the error.

I greatly appreciate any further guidance.

(P.S. I would much prefer to eliminate Zapier and use a fetch (I assume) to get the data directly from the Google sheet and then use the save() operation to update a Wix collection, where this is set up as a scheduled job. Are there examples here that I can study to accomplish this? )
(P.P.S.–It would be great if the standard import/export options for Collections included this kind of scheduled synch with Google sheets!)

Successful import script that used insert():

import wixData from 'wix-data';
import {ok, response} from 'wix-http-functions'
export function post_pgTypeA(request) {
    return request.body.json()
        .then(body => {
        let recordInsert = {
            "title": body.title,
            "description": body.description,
            "imageUrl": body.imageUrl,request,
            "linkButtonText": body.linkButtonText,
            "linkButtonUrl": body.linkButtonUrl,
            "authorName": body.authorName,
            "authorEmail": body.authorEmail
        };
        return wixData.insert('pgTypeA', recordInsert)                   
           .then(result => ok({body: JSON.stringify(result)}))
            .catch(err => response({status: 500, body: err}));        
         }
    );
}

My faulty re-work to use save() based on the documentation link you provided:


import wixData from 'wix-data';
import {ok, response} from 'wix-http-functions';
export function post_pgTypeA(request) {
    return request.body.json()
       .then(body => {
        let recordInsert = {
            "title": body.title,
            "description": body.description,
            "imageUrl": body.imageUrl,
            "linkButtonText": body.linkButtonText,
            "linkButtonUrl": body.linkButtonUrl,
            "authorName": body.authorName,
            "authorEmail": body.authorEmail
        };
 return wixData.save('pgTypeA', recordInsert)
    .then ((results) => {
        let item = results;
    })
         .catch((err) => {
            let errorMsg = err;
            })
    }
    );
}

Your new code with the .save() function does not call the ok() function of the http-functions API which might be causing your problem. Also, note that if you are updating a record, then you will need the _id as part of the recordInsert object. See the .save() function docs for details.

Also, you might find the Example: Google Sheets NPM useful.

Thank you again for your responses. To give my script immediate access to the _id of existing rows, I have created a “uniqueId” text field in the source Google sheet, because my understanding is that the import script can set the value of the “_id” field in my Wix collection (in this case using the value in the uniqueID field).

However, in my test import, all the Google sheet fields were correctly imported into the collection, except for _id. It did not accept the text value I passed to it from the Google sheet uniqueID field and instead appears to have created its own system value.

Are special steps required to set the value of the _id field when inserting a new row?

Here again is my basic import script for just importing:


import wixData from 'wix-data';
import {ok, response} from 'wix-http-functions';

export function post_pgTypeA(request) {

 return request.body.json()
        .then(body => {

 let recordInsert = {
 "title": body.title,
 "description": body.description,
 "imageUrl": body.imageUrl,
 "linkButtonText": body.linkButtonText,
 "linkButtonUrl": body.linkButtonUrl,
 "authorName": body.authorName,
 "authorEmail": body.authorEmail,
 "_id": body.uniqueID
        };

 return wixData.insert('pgTypeA', recordInsert).then(result => {
 return ok({body: JSON.stringify(result)})})
         .catch(err => response({status: 500, body: err}));
        }
    );
}