In line with this post here , I’m trying to essentially achieve the same goal via sending the payload via Zapier.
The goal is to use the profileId sent by the payload to query the Wix collection (memberRegistration) and find the matching profileId in the Wix collection. Once the match is found, update the qrCode (it’s an image URL) from the payload to the qrCode in the Wix collection.
For example, this would search through each item in memberRegistration for profileId (12345). If found, it would update the qrCode (https://image-example.png) from the payload to the qrCode field for profileId 12345.
Although I believe the code below is correct, I keep getting a internal server error when sending the payload (there is only one record to look through for testing and the profileId matches the profileId of the payload). Can you see any issues with the code below?
export function post_updateCode(request) {
return request.body.json()
.then(body => {
wixData.query('memberRegistration')
.eq('profileId', body.profileId)
.find()
.then(res => {
if (res === 0) {// If profileId is not in Database
}
else {// If profileId is in Database
console.log(body.profileId)
console.log(res.items)
let count = $w("#registered").getTotalCount();
console.log("Total Count = ", count)
for (var i = 0; i < count; i++) {
let idUpdate = res.items[i];
console.log(res.items[i].profileId)
if (res.items[i].profileId === body.profileId){
console.log("Successful")
console.log(idUpdate)
idUpdate.qrCode = body.qrCode;
wixData.update("memberRegistration", idUpdate);
i = count + 1
}
else{console.log("Unsuccessful")}
}
}
})
})
}
Here’s the error below:
Any ideas why this might be happening?
Also take a look onto this example here…
@russian-dima Ha! Yes, I’ve been following your post as well to see if I can find anything wrong with mine too. The code I think is the culprit.
Things I’ve confirmed:
http-functions.js in Backend - check!
collection set to anyone for permissions - check!
confirmed the URL is https://client.wixsite.com/my-site/_functions/updateCode - check!
I have no idea why it’d be giving the server error when sending the payload unless there’s an error with the code maybe…
@russian-dima It’s a free site and according to the documentation it should include /my-site. I tried without and I get a “404” error instead of an “internal server” error.
@heatherzirajones Ok, in this case → CHECKED 
In my case it was a premium.
@russian-dima Hehe yes! The more green checks the better for this problem-solving.
I was reading yours intensely and double-checking the URL too. I have no idea what the deal is here! I was testing it for hours last night and now hours today and I can’t find what it is.
@heatherzirajones
I am surely not the best choice for help in this case, but take a look at this example one…
Perhaps you will find some differences between it and your own version.
import {created, serverError} from 'wix-http-functions';
import wixData from 'wix-data';
export function post_myFunction(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("myUserCollection", JSON.parse(body));
} )
.then( (results) => {
options.body = {
"inserted": results
};
return created(options);
} )
// something went wrong
.catch( (error) => {
options.body = {
"error": error
};
return serverError(options);
} );
}
And here another one, out of the mentioned example…
export function post_rating(request) {
let options = {
"headers": {
"Content-Type": "application/json"
}
};
// get the request body
return request.body.json()
.then((body) => {
return updateStatistics(body["_id"], body["rating"]);
})
.then((results) => {
options.body = {
"inserted": results
};
return created(options);
})
// something went wrong
.catch((error) => {
options.body = {
"error": error
};
return serverError(options);
});
}
async function updateStatistics(productId, rating) {
// Get the rating statistics for the current recipe from the "Ratings" collection.
let stats = await wixData.get('Ratings', productId);
// If statistics data already exist for this recipe:
if (stats) {
stats.rating += rating; // Add the new rating to the total rating points.
stats.count += 1; // increment to indicate one more rating
// Update the new product statistics in the "review-stats" collection.
return wixData.update('Ratings', stats)
}
//If no statistics data exists for this product, create a new statistics item.
stats = {
// Set the statistics item's ID to the current recipe's ID.
_id: productId,
// Set the statistics item's rating to the rating entered by the user.
rating: rating,
// Set the statistics item's ratings count to 1 because this is the first rating.
count: 1
};
// Insert the new recipe statistics item into the Ratings collection.
return wixData.insert('Ratings', stats)
}
Link: Velo: Exposing a Site API with HTTP Functions | Help Center | Wix.com
Got it to work by getting on the right track with the suggestions of @russian-dima . The solution was a combination of that as well as using toUpdate:
export function post_codeWorks(request) {
let options = {
"headers": {
"Content-Type": "application/json"
}
};
// get the request body
return request.body.json()
.then((body) => {
return updateProfile(body["_id"], body["qrCode"]);
})
.then((results) => {
options.body = {
"inserted": results
};
return created(options);
})
// something went wrong
.catch((error) => {
options.body = {
"error": error
};
return serverError(options);
});
}
async function updateProfile(profileId, qrCode) {
wixData.get("memberRegistration", profileId)
.then((toUpdate) => {
toUpdate.qrCode = qrCode;
wixData.update("memberRegistration", toUpdate)
.then((result) => {
// updated object details
console.log(result);
})
.catch((err) => {
let errorMsg = err;
});
})
}