我正在测试新的@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)
})();
})();当我运行从.js文件编译的.ts文件时,我会得到以下响应:
authentication to c8y server然后死刑就停止了。
线
console.log('result from authetication', client)从来没有被召唤过。似乎在身份验证过程中出现了一些失败,而没有显示错误。
我做错什么了?
谢谢。
发布于 2018-07-05 20:29:29
第一个问题可能是CORS。如果您想要从另一个域请求,则需要启用它。这是一个指南如何在Cumulocity中这样做:
在“访问控制”下,管理员可以在Cumulocity上启用跨源资源共享或"CORS“。
第二个问题可能是您是,而不是从本地开发服务器运行它的。我主要使用这个http-来自npm的服务器来快速测试脚本。您可以通过以下方式使用它:
$ npm install http-server -g
$ http-server如果所有这些都无助于您尝试捕获客户端来查看它正在抛出的错误:
try {
const client = await Client.authenticate({
user,
password,
tenant
}, baseUrl);
} catch(ex) {
console.log(ex);
}exeption可能会告诉您更多关于代码错误的信息,或者客户端中是否存在错误。
https://stackoverflow.com/questions/51052018
复制相似问题