在 DialogFlow 内联编辑器中使用 'request-promise-native' 包
Use 'request-promise-native' package in DialogFlow inline editor
我正在向网络服务发出 POST 请求。 POST 请求的代码保存在一个单独的 function.The 函数调用中,从我的意图函数到外部函数是 asynchronous.So 我正在使用 Promises 来实现 synchronicity.The 这里的问题是当我在内联编辑器中导入 request-promise-native
时,它抛出错误 TypeError: Cannot read property 'prototype' of undefined
.
但是当我在具有节点 js 版本 9.11.1
的本地工作站中尝试它时它工作 fine.The DialogFlow 中的节点 JS 版本是 >=6.0
。是否有任何其他依赖项必须被添加到它?
谁能解释一下为什么会这样?
更新:
我将节点引擎更改为6.14.3
,并将package.json中模块'request-promise-native'的依赖项更改为"request-promise-native":"v1.0.5"
。但仍然没有luck.The,下面是我的代码:
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 );
});
});
}
我在内联部署代码后抛出错误 editor.The 错误是:
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)
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" 计划,但有一个免费套餐足以满足大多数开发和测试目的。
我正在向网络服务发出 POST 请求。 POST 请求的代码保存在一个单独的 function.The 函数调用中,从我的意图函数到外部函数是 asynchronous.So 我正在使用 Promises 来实现 synchronicity.The 这里的问题是当我在内联编辑器中导入 request-promise-native
时,它抛出错误 TypeError: Cannot read property 'prototype' of undefined
.
但是当我在具有节点 js 版本 9.11.1
的本地工作站中尝试它时它工作 fine.The DialogFlow 中的节点 JS 版本是 >=6.0
。是否有任何其他依赖项必须被添加到它?
谁能解释一下为什么会这样?
更新:
我将节点引擎更改为6.14.3
,并将package.json中模块'request-promise-native'的依赖项更改为"request-promise-native":"v1.0.5"
。但仍然没有luck.The,下面是我的代码:
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 );
});
});
}
我在内联部署代码后抛出错误 editor.The 错误是:
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)
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" 计划,但有一个免费套餐足以满足大多数开发和测试目的。