Missing 'Cache Key'...?

Hello,

I am trying to complete the lesson named “Invoke the API service Using Generated client SDK” lesson in the ‘Missions’. I am using ‘fetch’ to make the HTTP requests to the API endpoint URLs.

I am getting the following error when attempting to call the ‘getItems’ endpoint:

“code: 0,
message: ‘Cache Key must be provided and must be a string.’,
errorData: {}”

What does this ‘cache Key’ refer to??? There seems to be no mention of it in the “Cloud Code” console.

My fetch HTTP request for this endpoint call is structured like so:

 fetch('https://awaitedbranch.backendless.app/api/services/CodelessShoppingCartService/Items', { 
  method: 'GET',
  headers: { 'Content-Type': 'application/json', 'Accept': 'application/json' },
  }),

Thanks in advance. Regards.

Hi, @Stephen_Hamburger

In order to complete the mission, it is important to follow the steps specified in the mission description. Have you seen the video attached to the mission description? Mark demonstrates how to complete the mission using the JS SDK. I would suggest you take a look at it.

The error you got is due to a missing cartName parameter. It should look like this:

fetch("https://xxx.backendless.app/api/services/CodelessShoppingCartService/Items?cartName=support", {
  "headers": {
    "accept": "application/json",
    "content-type": "application/json"
  },
  "method": "GET"
});

By the way, you don’t need to make this request to complete the task.

Regards,
Marina

Greetings Marina,

Thank you for the reply and information. Actually the ‘getItems’ is mentioned as an endpoint that is to be invoked. I have watched the video 2-3 times and I am at a complete loss as to how to make it work. The HTML/JS files within the downloaded file need to be run on a server…for example here is the HTML page I see when I open it:

After making the fix you suggested to my ‘fetch’ statement(s)…I was able to execute all of them to the proper endpoints without errors. I received back the entire ‘instructions’, and then 3 ‘null’ responses…which I believe should indicate success. However I have not been given credit for completion of the task. What am I to do here? I thank you in advance.

Regards

Steve

Just to add some more information, I have done a bit of work to setup an environment to run this code on my localhost server…as can be seen here:

And when I run the code…the errors on the right side of the browser window are indicated after each link I press.

Basically I have absolutely no idea how this ‘SDK’ is to be run or the mission is to be completed. The video mentions an ‘IDE’ however I just use Notepad and setup a Node.js environment to run on localhost (as the first picture indicates) to run server code…

And again as I stated in the previous post I have been able to connect to the API using ‘fetch’ HTTP requests (and received responses back) however it does not seem to credit me for ‘touching’ the API endpoints.

Regards

Hello again,

I am still struggling with this ‘Cache Key’. Here are my complete ‘fetch’ statements:

     Promise.all([
	fetch('https://awaitedbranch.backendless.app/api/services/CodelessShoppingCartService/Instructions', { 
      method: 'GET',
      }),
	fetch('https://awaitedbranch.backendless.app/api/services/CodelessShoppingCartService/Item?cartName=support', { 
      method: 'POST',
      body: JSON.stringify({ "cartName": "mycart", "item": { "name": "Milk", "quantity": 1  }  }),
      }),
	fetch('https://awaitedbranch.backendless.app/api/services/CodelessShoppingCartService/Items?cartName=mycart', {
      method: 'GET',

      }),
	fetch('https://awaitedbranch.backendless.app/api/services/CodelessShoppingCartService/purchase?cartName=support', { 
      method: 'POST',

      }),
...

And here is the output from these fetch calls:

As can be seen, everything seems to return fine except the last fetch call…the one for ‘purchase’. It is stated a ‘Cache Key’ must be provided. I have tried BOTH of the following:

https://awaitedbranch.backendless.app/api/services/CodelessShoppingCartService/purchase?cartName=support
https://awaitedbranch.backendless.app/api/services/CodelessShoppingCartService/purchase?cartName=mycart

Neither of these keys are working. Please provide the necessary information for me to complete these fetch calls…there is nothing about these keys mentioned in the “API Services” console. As can be seen from my output, it seems all other information is returned correctly.

Regards

In your second call you’re adding an item to a cart named support, but in the 3rd call you’re fetching the contents of a cart named mycart (which doesn’t exist since there is nothing in it). That call results in the error you’re getting.

Regards,
Mark

Greetings Mark,

I thank you for your response. Here are my fetch statements as changed to use ‘mycart’:

     Promise.all([
	fetch('https://awaitedbranch.backendless.app/api/services/CodelessShoppingCartService/Instructions', { 
      method: 'GET',
      }),
	fetch('https://awaitedbranch.backendless.app/api/services/CodelessShoppingCartService/Item?cartName=support', { 
      method: 'POST',
      body: JSON.stringify({ "cartName": "mycart", "item": { "name": "Milk", "quantity": 1  }  }),
      }),
	fetch('https://awaitedbranch.backendless.app/api/services/CodelessShoppingCartService/Items?cartName=mycart', {
      method: 'GET',
      }),
	fetch('https://awaitedbranch.backendless.app/api/services/CodelessShoppingCartService/purchase?cartName=mycart', { 
      method: 'POST',
      }),

Resulting output from these calls, you can what is returned…interestingly the contents of the shopping cart (1 quantity of Milk) are NOT returned (as they had in the code I posted above):

Now if I change the fetch statements, as here:

     Promise.all([
	fetch('https://awaitedbranch.backendless.app/api/services/CodelessShoppingCartService/Instructions', { 
      method: 'GET',
      }),
	fetch('https://awaitedbranch.backendless.app/api/services/CodelessShoppingCartService/Item?cartName=mycart', { 
      method: 'POST',
      body: JSON.stringify({ "cartName": "mycart", "item": { "name": "Milk", "quantity": 1  }  }),
      }),
	fetch('https://awaitedbranch.backendless.app/api/services/CodelessShoppingCartService/Items?cartName=mycart', {
      method: 'GET',
      }),
	fetch('https://awaitedbranch.backendless.app/api/services/CodelessShoppingCartService/purchase?cartName=mycart', { 
      method: 'POST',
      }),

Resulting output from these calls, identical to the screenshot above:

And of course, in both cases I am sent back an error about the cache key in the final (purchase) call. So it actually doesn’t seem to matter what the string in the fetch URL states. The documentation in the “cloud code” console in fact doesn’t even mention their usage at all. The ‘CURL’ statements given there do not include these ‘cache keys’.

Is there some particular IDE I should install to run the (downloaded) code within? The video doesn’t seem to give any specific information on what environment is used to run the supplied code.

I thank you in advance.

Hello @Stephen_Hamburger

You must pass cartName in the body of the “purchase” request. And you pass cartName in the query according to your example.

Regards,
Inna

Greetings Inna,

Thank you for the information. I have changed my ‘fetch’ to move the cartName into the body of the ‘purchase’ request, as so:

     Promise.all([
	fetch('https://awaitedbranch.backendless.app/api/services/CodelessShoppingCartService/Instructions', { 
      method: 'GET',
      }),
	fetch('https://awaitedbranch.backendless.app/api/services/CodelessShoppingCartService/Item?cartName=mycart', { 
      method: 'POST',
      body: JSON.stringify({ "cartName": "mycart", "item": { "name": "Milk", "quantity": 1  }  }),
      }),
	fetch('https://awaitedbranch.backendless.app/api/services/CodelessShoppingCartService/Items?cartName=mycart', {
      method: 'GET',

      }),
	fetch('https://awaitedbranch.backendless.app/api/services/CodelessShoppingCartService/purchase', { 
      method: 'POST',
      body: JSON.stringify({ "cartName": "mycart" }),
      }),

And the error has cleared, as seen here:

Despite the HTTP requests being made without any errors, I am not receiving any credit for completing the mission…probably because the ‘order’ table is NOT created in my database console. What could be the reason for that?

Regards,

Steve

probably because the ‘order’ table is NOT created in my database console
Hello @Stephen_Hamburger,

yes, this is the problem.
Btw did you turn off the Dynamic Schema Definition in the tables settings?

Regards,
Stanislaw

Greetings Stanislaw,

No, I did not turn off the Dynamic Schema Definition…the toggle is to the right and green (enabled).

Regards

Steve

Hello Stephen!

Maybe the problem is that you have a broken order of execution and this can be because Promise.all does not guarantee that the requests will be executed in the order in which you pass them.

Regards,
Alexander

So did you try to create the Order table?

Greetings,

Alexander’s response seemed logical, so I broke each ‘fetch’ call into a separate ‘timeout’ with a 250 ms delay, as so:

setTimeout(function() {

         fetch(`https://awaitedbranch.backendless.app/api/services/CodelessShoppingCartService/Instructions`,  {
             method: 'GET',

             })
         .then(res => res.json())  //expecting a json response
         .then(data => {
         console.log(data);
         })				 
         .catch(err => {
         console.log('Error: ' + err);
         });

}, 250);

...etc with remaining 3 fetch calls

However, unsurprisingly, that did not resolve the mission.

I also attempted to run each ‘fetch’ statement individually…running my code 4 different times with each run using the ‘next’ necessary ‘fetch’ statement…

Of course that did not work. I then took Stanislaw’s response and created the ‘Order’ table manually, and input the ‘Milk’ with a quantity of “1”…that did not resolve the mission either.

To be honest, after many hours of work on this 1 mission…I am at the end. I see no point in continuing to smash my head against a brick wall. I even got a new ‘badge’ for making 100 API calls…that is how much I put into trying to complete this (impossible) task. I have no intention of wasting my time any more…I have wasted a week on this already.

As mentioned in a previous post besides using the approach with ‘fetch’ statements I did some work to setup a Node.js environment, ensured the paths in the HTML file were all properly linked to files in my folders, downloaded necessary modules, wrote server code to run on my ‘localhost’ environment…and as in the picture I posted all the links are non-functional.

I don’t know what else to do except that “I surrender…I give up”.

Hi @Stephen_Hamburger,

We are here to help you and try to understand what is going wrong. We’re sorry this is irritating you and that you’re at a dead end. More than 1500 developers have already successfully completed this mission, so I tried to complete the mission in your application and I succeeded the first time. I just followed the steps as instructed. It’s hard for me to say exactly where you probably missed something, but in any case, the mission is completed, the badge is yours. You can continue and I hope that it will be a little easier and without issues for you.

Regards,
Stanislaw

Greetings Stanislaw,

I thank you for your time and help (along with with the other posters). And the gesture is much appreciated.

Regards

Steve

1 Like