get http-functions

Help! I don’t know why it cannot find the item in the Collection it keeps saying not found when the url is run. I’ve attached an image of the Live database it is trying to access and the code below:

//https://epdpharm.com/_functions/myFunction/John

export function get_myFunction(request) {
return wixData.query(“myUsersCollection”)
.eq(“firstName”, request.path[0])
.find()
.then( (results) => {
if(results.items.length > 0){
const body = “Found it!”;
return ok({body: body});
}
const body = “Not in the system”;
return notFound({body: body});

	}); 

}

Hi,

The Field Key for the firstName field is title . So…

Not this:
.eq(“firstName”, request.path[0])

But this:
.eq(“title”, request.path[0])

I hope this helps.

Yisrael

Oh its the Field Key for the first part I thought it was the column title! thanks Yisrael, you solved my issue :slight_smile:

By chance Yisrael, do you know how to get a specific value in the column next to the item I’m searching? So one column is “title” aka firstName for me and the second is “lastName” and I want to know the lastName that is associated with the firstName column.

The results that are returned are rows, so you can get all of the fields that you want from the returned results. I’ve added a couple of lines to your code to illustrate:

export function get_myFunction(request) {
   return wixData.query("myUsersCollection")
      .eq("firstName", request.path[0])
      .find()
      .then((results) => {
         if (results.items.length > 0) {
            const body = "Found it!";
            let item = results.items[0]; // get first row of results
            let lastName = item.lastName;// get desired field
            return ok({ body: body });
         }
         const body = "Not in the system";
         return notFound({ body: body });
      });
}

I hope this helps.

Yisrael

How do I display “item” and “lastName” in the results? when I run that it will only show ‘Found it!’

You will need to add components on your page to display the retrieved data. See the tutorial How to Create a Custom Form & Connect It to a Database for step-by-step explanation.

Another good tutorial is How to Use Code to Let Your Users Search a Collection .

Have fun,

Yisrael

I have all those components on my website already for the website users to add the info into the databases, what I need is to display the information to the third party who is calling the url function to see what they added cause I don’t want to display the “_owner”, “_id”,etc hidden columns when I print the results.item

See the article Exposing a Site API with HTTP Functions for an explanation of the techniques used to return an HTTP request. For full details, see the wix-http-functions API.

The exposing link doesn’t go to a site and the other doesn’t show how to do a slot inside the selected row the user found in the url

Sorry, for some reason the link was corrupted. I fixed it in my post above.
Here it is again: Exposing a Site API with HTTP Functions

is there anyway to format how it shows the data? neither link shows how to do a newline for each data slot in the row

I don’t understand why you need to format the data - I thought that it’s being passed in an HTTP request. The way you pass the data is up to you. You just need to follow the guidelines in Exposing a Site API with HTTP Functions .

I don’t have an issue with how it is passed through its how it comes out after finding the results I was hoping to change so it would not show the “_id”, “_owner”, “_updatedDate” and “_createdDate” information in the results row

Hi, can anyone please tell me how to send an authorization header with a get request? I am making the request as
mywebsite.com is available for purchase - Sedo.com and my get_Menus function is within my http-functions.js files as:
import {ok, notFound, serverError} from ‘wix-http-functions’;
import wixData from ‘wix-data’;

export function get_Menus(request) {

let options = {
“headers”: {
“Content-Type”: “application/json”
}
};
// query a collection to find matching items
return wixData.query(“Menus”)

.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]} ${request.path[1]}' was not found` 
  }; 
  return notFound(options); 
} ) 
// something went wrong 
.catch( (error) => { 
  options.body = { 
    "error": error 
  }; 
  return serverError(options); 
} ); 

}

Thanks in advance!

UPDATE: thanks to another post on the Forum for an unrelated issue I’ve found that I required the “suppressAuth”: true , “suppressHooks”: true in the
let options = {} syntax so didn’t require any authorization headers or anything else to overcome my 404 not found issue