2

I am testing the new @c8y/client library for typescript.

I have a very simple code :

import {
  Client
} from '@c8y/client';
//const baseUrl = 'https://bismark1.cumulocity.com/';
const baseUrl = 'https://demos.cumulocity.com/';
const tenant = 'bismark1';
const user = '...';
const password = '.....';


(async() => {
  console.log('authentication to c8y server')
  const client = await Client.authenticate({
    user,
    password,
    tenant
  }, baseUrl);

  console.log('result from authetication', client)
  const {
    data,
    paging
  } = await client.inventory.list();
  console.log('result from inventory ', data)

  // data = first page of inventory
  const nextPage = await paging.next();
  // nextPage.data = second page of inventory

  const managedObjId: number = 1;

  (async() => {
    const {
      data,
      res
    } = await client.inventory.detail(managedObjId);
    console.log(data)
  })();

})();

When I run the .js compiled form the .ts file I get the response below :

authentication to c8y server

And then the execution stops.

The line

console.log('result from authetication', client)

is never called. Seems like something fails in the authentication process and not error is showed.

What I'm doing wrong ?

Thanks.

Jorge
  • 238
  • 1
  • 10

1 Answers1

2

The first problem might be CORS. You need to enable it if you want to request from a different domain. Here is a guide how to do that in Cumulocity:

Under "Access control", administrators can enable cross-origin resource sharing or "CORS" on the Cumulocity API.

The second problem could be that you are not running it from a local development server. I mostly use this http-server from npm to quickly test scripts. You can use it the following way:

$ npm install http-server -g
$ http-server

If that all is not helping you might try catch the client to see the error it is throwing:

try {
  const client = await Client.authenticate({
    user,
    password,
    tenant
  }, baseUrl);
} catch(ex) {
 console.log(ex);
}

The exeption might tell you more about what is wrong with your code or if there is a bug in the client.

Jan Hommes
  • 5,122
  • 4
  • 33
  • 45
  • I have same problem, and I have enabled Access Control and have local webpack dev server. Still I get cors warning / issue. – Shnigi Dec 19 '18 at 12:09
  • Access to fetch at 'https://cumulocityplatformURL/tenant/currentTenant' from origin 'http://localhost:8080' has been blocked by CORS policy: Request header field pragma is not allowed by Access-Control-Allow-Headers in preflight response. I see Pragma in request headers and its not allowed in the backend, so is this webpack dev server issue or Angular issue? – Shnigi Dec 19 '18 at 12:48
  • this is a bug @c8y/client and was fixed in 9.19.0. Can you update the client to that version `npm install @c8y/client@9.19.0` and check again? – Jan Hommes Dec 19 '18 at 12:59
  • Yes that did it! Thanks a lot for quick reply. I'm amazed. – Shnigi Dec 19 '18 at 13:22
  • do you know how can I store c8y/client login info to be used in another components? Should I store login info to localstorage or how can I use logged client in elsewhere? The documentation is bit plain. – Shnigi Dec 20 '18 at 10:34
  • Yep, local storage would work. Cumulocity itself uses angular service to store the information at application runtime and for permanent local or session storage. – Jan Hommes Dec 27 '18 at 14:18