Get function to retrieve database row with particular value

Wanted to write http function with GET function as per https://www.wix.com/corvid/reference/wix-http-functions.html#get

Database values is noted as below:

Code is written as below:

'use strict';
import wixData from 'wix-data';
import wixHttpFunctions from 'wix-http-functions';
import {ok, response, notFound, serverError} from 'wix-http-functions';

export function get_fetchrecords(request) {

let options = {
"headers": {
"Content-Type": "application/json"
}
};

// query a collection to find matching items
return wixData.query("SampleCollection")
.eq("title", request.path[0])

.find()
.then( (results) => {
// matching items were found
if(results.items.length > 0) {
options.body = {
"items": results.items
};
return ok(options);
}
// no matching items found
options.body = {
"error": `'${request.path[0]}' was not found`
};
return notFound(options);
} )
// something went wrong
.catch( (error) => {
options.body = {
"error": error
};
return serverError(options);
} );
}

I am trying to using GET function in external app to pull the data from row with title value as “3” by using https://wixtozapier.com/_functions/fetchrecords?&title=3

Instead of getting only the row with title value 3, I get all the records from the database.

How can I rectify this to get only the required row?

Check the HTTP Functions and the get function again as you missed out a simple part of it.
(I can say that after I had to have a think about it too :wink:)

// In http-functions.js

import {ok, notFound, serverError} from 'wix-http-functions';
import wixData from 'wix-data';
// URL looks like:
// https://www.mysite.com/_functions/myFunction/John/Doe
// or:
// https://user.wixsite.com/mysite/_functions/myFunction/John/Doe

So go back to your link in your post…

https://wixtozapier.com/_functions/fetchrecords?&title=3
This is returned:
{“items”:[{“number”:“2383”,“_id”:“1f007c60-10f9-4092-ae54-29212fe4778c”,“_createdDate”:“2020-01-14T10:39:10.189Z”,“_updatedDate”:“2020-01-14T10:39:10.189Z”,“color”:“White”,“lastName”:“Doe”,“firstName”:“John”,“title”:“2”},{“number”:“1212”,“_id”:“e6dfcca4-8c4a-4228-b65b-ca393bf6811b”,“_createdDate”:“2020-01-14T10:29:09.286Z”,“_updatedDate”:“2020-01-15T12:42:49.675Z”,“color”:“Blue”,“lastName”:“Enterprises”,“firstName”:“Acme”,“title”:“3”}]}

Whereas if you change the url link to match the example…

https://wixtozapier.com/_functions/fetchrecords/3
This is returned:
{“items”:[{“number”:“1212”,“_id”:“e6dfcca4-8c4a-4228-b65b-ca393bf6811b”,“_createdDate”:“2020-01-14T10:29:09.286Z”,“_updatedDate”:“2020-01-15T12:42:49.675Z”,“color”:“Blue”,“lastName”:“Enterprises”,“firstName”:“Acme”,“title”:“3”}]}

Just to double check too, with the other one too…

https://wixtozapier.com/_functions/fetchrecords/2
This is returned:
{“items”:[{“number”:“2383”,“_id”:“1f007c60-10f9-4092-ae54-29212fe4778c”,“_createdDate”:“2020-01-14T10:39:10.189Z”,“_updatedDate”:“2020-01-14T10:39:10.189Z”,“color”:“White”,“lastName”:“Doe”,“firstName”:“John”,“title”:“2”}]}

Ah damn. Knew I was doing something wrong but simple. Thanks buddy.

Just a follow up now that I’ve got the proper “_id” from the GET command, I am trying to replace the values from an external db in the wix database through a PUT function, but its not working for me. Keep getting 500 error. Code is below. Can you see something wrong in that too?

export function put_updaterecords (request) {

return request.body.json()
.then(body => {
let recordUpdate = {
"_id": body.ID,
"title": body.Title, 
"firstName": body.FirstName,
"lastName": body.LastName,
"color": body.Color,
"number": body.Number
};
return wixData.update("SampleCollection", recordUpdate)
.then(result => ok({body: JSON.stringify(result)}))
.catch(err => response({status: 500, body: err}));
})
}