【问题标题】:Use 'request-promise-native' package in DialogFlow inline editor在 DialogFlow 内联编辑器中使用“request-promise-native”包
【发布时间】:2018-06-23 18:20:22
【问题描述】:

我正在向网络服务发出 POST 请求。 POST请求的代码保存在一个单独的函数中。从我的意图函数到外部函数的函数调用是异步的。所以我使用Promises来实现同步。这里的问题是当我在我的内联中导入request-promise-native编辑器抛出错误为TypeError: Cannot read property 'prototype' of undefined

但是当我在我的本地工作站中尝试它时,它的节点 js 版本为 9.11.1,它运行良好。DialogFlow 中的节点 JS 版本是 >=6.0。是否需要添加任何其他依赖项?

谁能解释一下为什么会这样?

更新: 我将节点引擎更改为6.14.3,并将package.json中模块'request-promise-native'的依赖更改为"request-promise-native":"v1.0.5"。但仍然没有运气。以下是我的代码:

var doco;
 var rp = require('request-promise-native');
 var myJSONObject = {
  "inputs" : [  {
    "name" : "<name>",
    "value" : <value>
  } ]
};
var orchName = 'TEST05';
  postData = JSON.stringify(myJSONObject);
 return networkCall(postData,orchName).then((response)=>{
			console.log('response is'+response)            
             console.log("+++++++++++++=DOCO=+++++++++ "+response);
             doco = doco1;
             //agent.add(`Order number is ${doco1}`);
             
        }).catch((response) => {
      console.log(`ERROR: `+response);
    });
	
console.log('doco'+doco);	

function networkCall(postData, orchName) {
    return new Promise((resolve,reject) =>{
      var options = {
  method: 'post',
  uri: '<URL>',
  body: myJSONObject,
  auth: {
        'user': 'usr',
        'pass': 'pwd'
      },
  json: true
};
return rp( options )
  .then( body => {
	 // var test = JSON.stringify(body)
	 var doco =body.ServiceRequest1.subforms.z_DOCO_137.value;
	console.log('DOCO '+doco);
    resolve( doco );
  })
  .catch( err => {
	  console.log('FAILED'+err);
    reject( err );
  });
    });
}

当我在内联编辑器中部署代码时,就会引发错误。错误是:

The deployment of your Cloud Function failed:
Function load error: Code in file index.js can't be loaded.
Is there a syntax error in your code?
Detailed stack trace: TypeError: Cannot read property 'prototype' of undefined
at module.exports (/user_code/node_modules/request-promise-native/node_modules/request-promise-core/configure/request2.js:34:47)
at Object.<anonymous> (/user_code/node_modules/request-promise-native/lib/rp.js:15:1)
at Module._compile (module.js:577:32)
at Object.Module._extensions..js (module.js:586:10)
at Module.load (module.js:494:32)
at tryModuleLoad (module.js:453:12)
at Function.Module._load (module.js:445:3)
at Module.require (module.js:504:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/user_code/index.js:17:23)

【问题讨论】:

  • Dialogflow 编辑器使用的节点版本是 6.14.3。尝试使用该版本进行测试,看看是否可以重现错误。如果是这样,请使用您正在使用的 request-promise-native 版本、代码的最小示例以及您收到的完整错误消息来更新您的问题。
  • 我已经更新了我的问题!

标签: javascript node.js actions-on-google dialogflow-es


【解决方案1】:

request-promise-native 包需要 request 包作为共同依赖项。因此,您需要将其显式添加到 Dialogflow 编辑器的 package.json 选项卡中。

这是适合我的package.json

{
  "name": "dialogflowFirebaseFulfillment",
  "description": "This is the default fulfillment for a Dialogflow agents using Cloud Functions for Firebase",
  "version": "0.0.1",
  "private": true,
  "license": "Apache Version 2.0",
  "author": "Google Inc.",
  "engines": {
    "node": "~6.0"
  },
  "scripts": {
    "start": "firebase serve --only functions:dialogflowFirebaseFulfillment",
    "deploy": "firebase deploy --only functions:dialogflowFirebaseFulfillment"
  },
  "dependencies": {
    "actions-on-google": "2.0.0-alpha.4",
    "firebase-admin": "^4.2.1",
    "firebase-functions": "^0.5.7",
    "dialogflow": "^0.1.0",
    "dialogflow-fulfillment": "0.3.0-beta.3",
    "request-promise-native": "^1.0",
    "request": "^2.87"
  }
}

不要忘记,除了拥有正确的软件包外,您还需要将项目升级为付费帐户。免费的 Firebase“Spark”计划不允许访问 Google 网络之外的网络。您可以升级到即用即付的“Blaze”计划,但有一个足以满足大多数开发和测试目的的免费套餐。

【讨论】:

  • 感谢Package.json 文件。现在我可以使用request-promise-native 模块。但现在的问题是,Google 助手没有等待从被调用函数返回的承诺。来自被调用函数的响应大约需要 7 秒才能完成。有没有办法让 Intent 处理程序等待响应?
  • 对此有很多答案,具体取决于最好作为附加问题解决的一些细节。我建议你问一个新的 StackOverflow 问题,提供你正在使用的代码,包括意图处理程序函数设置,你不包括在这里。如果这个答案有效,接受和/或支持它总是值得赞赏的。
  • 我想通了。我优化了我的代码,使我的外部函数运行时间不超过 5 秒。从函数到意图处理程序的返回值现在通过文本和语音发送给用户。非常感谢。没有你的package.json 文件是不可能的。
  • 很高兴你能成功!如果答案有帮助,欢迎接受和/或支持它。
猜你喜欢
  • 1970-01-01
  • 2018-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-29
  • 2020-11-13
  • 1970-01-01
相关资源
最近更新 更多