Hey, I have gone through documentation where someone can use http functions to create an API method for his wix website.
I see that one can use put method update existing collection
use that code to update my own collection but when I call that method from browser I get the 404 page not found error.
I don’t know why this happens and I also don’t know how the data should be passed.
import wixData from ‘wix-data’;
import {ok, response, serverError} from “wix-http-functions”;
export function put_updatemycollection(request) {
let options = {
“headers”: {
“Content-Type”: “application/json”
}
};
return request.body.text()
.then( (body) => {
// update the item in a collection
return wixData.update(“collection”, JSON.parse(body));
} )
.then( (results) => {
options.body = {
“inserted”: results
};
return ok(options);
} )
// something went wrong
. catch ( (error) => {
options.body = {
“error”: error
};
return serverError(options);
} );
}
Please help me to solve this problem
Hey there!
Always send JSON from Zapier, it will be the default if you don’t change anything. I have hundreds of functions that work from Zapier. Here is one for you to play with and use POST from Zapier, PUT is the standard for updating but it does not matter as long as you don’t use GET.
below code will execute by posting JSON to https://www.domain.com/_functions/addcontent/
export function post_addcontent(request) { // POST = Method
return request.body.json()
.then(body => {
let myContent = {
"title": body.title,
"description": body.description,
"source": body.source,
"type": body.type,
"image": body.image,
"link": body.link,
"category": body.category,
};
return wixData.insert('Data Collection Name', myContent)
.then(result => ok({body: JSON.stringify(result)}))
.catch(err => response({status: 500, body: err}));
}
);
}
Hope it works!
I want to update my collect I visited your link but your link also shows 404 error (page not found) and you are inserting hard coded content, how to pass content from zapier and how can I update a column of complete table
Hey
The url I supplied was only a sample url for you to see how you will call it. You cannot visit POST functions in a browser. The code I supplied do insert new rows every time Zapier is posting data to it. If you want to update data use .update in Wix but then you will also need to supply some _id so your function will know which row to update.
@andreas-kviby thanks for response but I don’t need to update specific record I need to update complete table (collection)
@engineerumairshah Hey
Ok then you will have to first delete all rows and then insert all new items. Is that correct?
@andreas-kviby No I want to update a column of existing collection don’t need to delete rows
@engineerumairshah
Hey
It’s kind of hard knowing exactly what you need and what you want.
“but I don’t need to update specific record I need to update complete table (collection)”
“No I want to update a column of existing collection don’t need to delete rows”
“how can I update a column of complete table”
If if have to try to guess again you want to update ONE specific column in the whole Data Collection with a value that comes from Zapier. If that is so then do the following.
This function will answer to POST only from Zapier and then it will loop through all your records and update the columns you want with the data from Zapier.
export function post_updateColumn(request) {
let dataCollectionName = "Data Collection Name here";
request.body.json()
.then(body => {
wixData.query(dataCollectionName)
.find()
.then((results) => {
let items = results.items;
items.forEach((item) => { // Loop through all data
item.columnFieldkey = body.fieldfromZapier;
// Sample
// item.title = body.newTitle;
wixData.update(dataCollectionName, item).then((updated) => {
// Record is now updated
})
})
})
});
}
@andreas-kviby thanks this was what i needed ( update ONE specific column in the whole Data Collection with a value that comes from Zapier )
but can you please let me know that same solution will work on 2 columns?
@engineerumairshah
// Sample
// item.fieldKey1 = body.newFieldValue1;
// item.fieldKey2 = body.newFieldValue2;
That will update two columns.