Search code examples
javascripttypescriptsdkwhere-clausecommercetools

commercetools nodejs sdk query with where or withWhere doesn't work


I'm currently new with commercetools. Using it's TypeScript SDK to start with, all looks fine following around but then I was playing around and trying to add where clause or withWhere so I can be more exact what to query when using get or post but does not work though.

I have been following this https://docs.commercetools.com/sdk/sdk-example-code#query-your-product and also using.

Without exposing too much keys and options I have created something like

    const client = clientObject
      .withProjectKey(options.projectKey)
      .withMiddleware(
        createAuthForClientCredentialsFlow(options.authMiddlewareOptions),
      )
      .withMiddleware(createHttpClient(options.httpMiddlewareOptions))
      .withUserAgentMiddleware()
      .build();

    const apiRoot = createApiBuilderFromCtpClient(client).withProjectKey({
      projectKey: options.projectKey,
    });

basically if I do

 const cart = getApiRoot()
      .carts()
      .withId({ ID: id })
      .get()
      .execute()

I will get what I need but if I want to add where I couldn't... I tried, which does not work

 const cart = getApiRoot()
      .carts()
      .withwhere(`"ID = ${id}"`)
      .get()
      .execute()

I also tried below

 const cart = getApiRoot()
      .carts()
      .get({
        queryArgs: {
          where: [`"ID = ${id}"`]
        }
      })
      .execute()

None of them would run properly, I wonder if someone can directly me to the right direction.

Thank you so much for you attention and time with advices / suggestions.


Solution

  • I'd recommend reading the Query Predicates page of the commercetools docs as it explains the queries that can be used for the where parameter. For Carts specifically, they list the fields that can be filtered here: https://docs.commercetools.com/api/predicates/query#on-carts

    Anyhow, regarding your examples above: the fields are case sensitive so that's why "ID" didn't return anything. Also, I'd generally recommend using withId() and withKey() if you know exactly what resource you want.

    Below are some working examples of using where on Carts. Note that the field to query doesn't need quotes, but the value does.

    Find all carts from a specific Customer Email:

    const cartsWithSpecificCustomerEmail = (customerEmail: string) => {
        return apiRoot
            .carts()
            .get(
                {
                    queryArgs:
                    {
                        where: `customerEmail="${customerEmail}"`
                    }
                }
            )
            .execute()
    }
    

    Find all carts with a shipping address in a specific city (please note the use of parentheses for fields within an object):

    const cartsWithShippingAddressToSpecificCity = (shippingAddressCity: string) => {
        return apiRoot
            .carts()
            .get(
                {
                    queryArgs:
                    {
                        where: `shippingAddress(city="${shippingAddressCity}")`
                    }
                }
            )
            .execute()
    }
    

    Using both of these filters in the same call:

    const cartsWithSpecifiedEmailAndShippingAddress = (customerEmail: string, shippingAddressCity: string) => {
        return apiRoot
            .carts()
            .get(
                {
                    queryArgs:
                    {
                        where: [
                            `customerEmail="${customerEmail}"`,
                            `shippingAddress(city="${shippingAddressCity}")`
                        ]
                    }
                }
            )
            .execute()
    }