我正在使用 IBM Cloud函数从 IBM Watson Discovery 请求一些结果.此处有一个很好的GitHub存储库关于如何调用此存储库.但是,在功能中设置动作的方式中,出现错误,消息为找不到模块'ibm-watson/auth'.我了解消息内容,但是如果您查看GitHub存储库,就会发现我执行了完全相同的步骤.我的IBM Cloud功能代码是:
I'm using IBM Cloud functions for requesting some results from IBM Watson Discovery. There is a very good GitHub repo here about how can we invoke this. However, on the way of setting the action in the function an error with message Cannot find module 'ibm-watson/auth' occurs. I understand what the message says, but if you check the GitHub repo, you see that I apply exactly the same steps. My IBM Cloud function code is:
case 'doc_info': var user_input = params.user_input; const watson = require('ibm-watson/sdk'); const { CloudantV1 } = require('@cloudant/cloudant'); const DiscoveryV1 = require('ibm-watson/discovery/v1'); const { IamAuthenticator } = require('ibm-watson/auth'); //const { IamAuthenticator } = require('ibm-cloud-sdk-core'); try { const discovery = new DiscoveryV1({ // Initializing Discovery version: '2020-11-24', authenticator: new IamAuthenticator({ apikey: params.apiKeyDisco, }), serviceUrl: params.urlDisco, }); const cloudant = new CloudantV1({ // Initializing CloudantV1 authenticator: new IamAuthenticator({ apikey: params.apiKeyCloudant }) }); cloudant.setServiceUrl(params.urlCloudant); // ... return { answer: "Simple test" }; } catch (err) { console.log(err) return Promise.reject({ statusCode: 500, headers: { 'Content-Type': 'application/json' }, body: { message: err.message }, }); } break;即使从 ibm-cloud-sdk-core 包中调用 IamAuthenticator ,我也尝试过,但是错误是 IamAuthenticator不是构造函数.我们该如何解决呢?
I've tried even with calling IamAuthenticator from the ibm-cloud-sdk-core package, but then the error is IamAuthenticator is not a constructor. How can we solve this?
推荐答案在此处查看 IBM Cloud Functions中可用的运行时环境.根据Node.js版本的不同,IBM Watson SDK的版本也有所不同.如文档中所述,Node.js 12运行时具有您正在寻找的具有IAM身份验证功能的较新SDK.Node.js 10运行时具有V4.x IBM Watson SDK.
See here for the runtime environments available in IBM Cloud Functions. Depending on the Node.js version, there are different versions of the IBM Watson SDK. As stated in the docs, the Node.js 12 runtime has the newer SDK with the IAM Authentication functionality you are looking for. The Node.js 10 runtime has the V4.x IBM Watson SDK.
因此,您需要使用以下旧方法(已记录)进行身份验证,或升级到Node.js 12运行时.
Thus, you need to either use the old (documented) way of authenticating, or move up to the Node.js 12 runtime.
如果您使用旧的用户名/密码身份验证,并且仅具有IAM API密钥,请使用 iamapikey 作为用户名,并使用API密钥作为密码.
If you use the old username / password authentication and only have an IAM API key, use iamapikey as username and the API key as password.
更多推荐
IBM Cloud Functions:找不到"ibm
发布评论