OR statement in Search Query

Whilst programming a function which is passed a projects ID or name, I’m using a post command which searches for a project according to the body I’ve written.
The body looks like this:

    let body = {
    query: {
      _or: [
        {name: {_like: nameOrId}},
        {id: {_eq: nameOrId}}        

The following is my call to the API:
await sdk.api().v2.post("/projects/search", body).

Now whenever I pass an ID to the function it works flawlessly but using a name (i.e. “Demo Project”) I receive the error 500 “unexpected server error occurred”.

When there’s only the one of the equal/ like statements no error occurs.

I’m using the following instance: https://api.calponia-beta.bosch.com
All the programming happens in Node.js using the Calponia SDK.

I greatly appreciate any help you can provide :slight_smile:

The id is an uuid (string with a special format). You can adjust your code to:

    const or = [
      {name: {_like: `%${nameOrId}%`}} // I guess you want like in a way of name includes string
    const uuidPattern = /^[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}$/
    if (nameOrId.match(uuidPattern)) {
      or.push({id: {_eq: nameOrId}})
    let body = {query: [{_or: or}]}

Then it will only search for ids, if the nameOrId is an id.

(I think on newer calponia-versions, the request would get an error-message, which says that :slight_smile: )

1 Like

Thank you Thomas!
I adapted your code a little to make it even easier:

const uuidPattern = /^[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}$/
if (nameOrId.match(uuidPattern)) {
   body = {query: {id: {_eq: nameOrId}}}
} else {
   body = {query: {name: {_like: nameOrId}}}

It works flawlessly. Thanks again :slight_smile: :+1: