Hello! I am trying to manage a membership database in wix, which is connected to google sheets. Currently I have the database and my google sheet connected through Zapier, but I am having trouble setting up the function in wix that handles the incoming data. This is what I have so far:
‘use strict’;
import wixData from ‘wix-data’;
import {ok, response} from ‘wix-http-functions’;
export function put_membership(request){
return request.body.json()
.then(body => {
recordInsert = {
“email”: body.Email,
“FirstNm”: body.FirstNm,
“SWEID”: body.SWEID,
“PaidUnpaid”: body.PaidUnpaid,
“Collected”: body.Collected,
“Total”: body.Total
};
wixData.query("MembershipData")
.eq("email", body.Email)
.find()
.then( (results) => {
//if user is not found
**if** (results.items.length === 0){
**return** wixData.insert("MembershipData", recordInsert)
.then(result => ok({body: JSON.stringify(result)}))
. **catch** (err => response({status: 500, body: err}));
}
**return** wixData.update("MembershipData", recordInsert)
.then(result => ok({body: JSON.stringify(result)}))
. **catch** (err => response({status: 500, body: err}));
});
})
}
What I am trying to do is take the incoming data, create a record insert out of it, query my database to see if I already have this member, and update their information if so. If the member’s email is not in my database, I would like to insert their information. Currently Zapier is returning an error when I test the functionality of this. If I were to get rid of the query, and only include ‘return wixData.insert(“MembershipData”, recordInsert)’ and the following few lines, the code works perfectly, and Zapier returns as a success! But, again, I need it to update if the member is already present in the database, not just insert a duplicate. Can anyone advise on what I need to change or fix to accomplish this goal?