如何使用@c8y/client库
How use the @c8y/client library
我正在测试新的@c8y/client 打字稿库。
我有一个非常简单的代码:
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)
})();
})();
当我 运行 从 .ts 文件编译 .js 时,我得到以下响应:
authentication to c8y server
然后执行停止。
行
console.log('result from authetication', client)
从未被调用。似乎身份验证过程中出现问题,并且未显示错误。
我做错了什么?
谢谢。
第一个问题可能是 CORS. You need to enable it if you want to request from a different domain. Here is a guide 如何在 Cumulocity 中做到这一点:
Under "Access control", administrators can enable cross-origin
resource sharing or "CORS" on the Cumulocity API.
第二个问题可能是您不是运行它来自本地开发服务器。我主要使用此 http-server from npm 来快速测试脚本。您可以通过以下方式使用它:
$ npm install http-server -g
$ http-server
如果这一切都没有帮助,您可以尝试捕获客户端以查看它抛出的错误:
try {
const client = await Client.authenticate({
user,
password,
tenant
}, baseUrl);
} catch(ex) {
console.log(ex);
}
该异常可能会告诉您更多有关您的代码有什么问题或客户端中是否存在错误的信息。
我正在测试新的@c8y/client 打字稿库。
我有一个非常简单的代码:
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)
})();
})();
当我 运行 从 .ts 文件编译 .js 时,我得到以下响应:
authentication to c8y server
然后执行停止。
行
console.log('result from authetication', client)
从未被调用。似乎身份验证过程中出现问题,并且未显示错误。
我做错了什么?
谢谢。
第一个问题可能是 CORS. You need to enable it if you want to request from a different domain. Here is a guide 如何在 Cumulocity 中做到这一点:
Under "Access control", administrators can enable cross-origin resource sharing or "CORS" on the Cumulocity API.
第二个问题可能是您不是运行它来自本地开发服务器。我主要使用此 http-server from npm 来快速测试脚本。您可以通过以下方式使用它:
$ npm install http-server -g
$ http-server
如果这一切都没有帮助,您可以尝试捕获客户端以查看它抛出的错误:
try {
const client = await Client.authenticate({
user,
password,
tenant
}, baseUrl);
} catch(ex) {
console.log(ex);
}
该异常可能会告诉您更多有关您的代码有什么问题或客户端中是否存在错误的信息。