问题描述
以下代码片段来自 watson 音调分析器文档。问题:当我粘贴代码并运行代码时,它运行得非常好。但是,当我将其粘贴到另一个云函数中时,它表明无法找到 ibm-watson/auth 模块。不知道为什么它的行为不同??
通常我保存在函数 main (params){} 下。但是,另一个云函数是 async 函数,它具有 (require-promise) 的代码。不知道出了什么问题或出了什么问题?
const ToneAnalyzerV3 = require('ibm-watson/tone-analyzer/v3');
const { IamAuthenticator } = require('ibm-watson/sdk');
const toneAnalyzer = new ToneAnalyzerV3({
version: '2017-09-21',authenticator: new IamAuthenticator({
apikey: 'apikey',}),serviceUrl: 'url',});
const text = 'Team,I kNow that times are tough! Product '
+ 'sales have been disappointing for the past three '
+ 'quarters. We have a competitive product,but we '
+ 'need to do a better job of selling it!';
const toneParams = {
toneInput: { 'text': text },contentType: 'application/json',};
toneAnalyzer.tone(toneParams)
.then(toneAnalysis => {
console.log(JSON.stringify(toneAnalysis,null,2));
})
.catch(err => {
console.log('error:',err);
});
}
解决方法
使用 IBM Cloud Functions 时,请注意不同的 runtime environments 及其包含的模块和包。如果您遇到找不到某个模块的错误,您
- 要么必须使用更高版本的更新运行时环境
- 或者必须include the package / module on your own。