您最后的猜测是正确的-问题是您没有使用Promises。
app.intent()``userInfo如果使用异步调用,则期望处理程序函数(在您的情况下)返回Promise。(如果不是,则可以不返回任何内容。)
正常的操作过程是使用返回Promise的东西。但是,这对于您而言是棘手的,因为尚未将地理代码库更新为使用Promises,并且userInfo函数中的其他代码未返回任何内容。
在这种情况下,重写可能看起来像这样(但是,我还没有测试代码)。在其中,我将两个条件userInfo分解为另外两个函数,以便一个可以返回Promise。
function userInfoNotFound( conv, params, granted ){ // Note: Currently, precise locaton only returns lat/lng coordinates on phones and lat/lng coordinates // and a geocoded address on voice-activated speakers. // Coarse location only works on voice-activated speakers. conv.ask(new SimpleResponse({ speech:’Sorry, I Could not find you’, text: ’Sorry, I Could not find you’ })) conv.ask(new Suggestions([’Locate Me’, ’Back to Menu’,’ Quit’]))}function userInfoFound( conv, params, granted ){ const permission = conv.arguments.get(’PERMISSION’); // also retrievable with explicit arguments.get console.log(’User: ’ + conv.user) console.log(’PERMISSION: ’ + permission) const location = conv.device.location.coordinates console.log(’Location ’ + JSON.stringify(location)) return new Promise( function( resolve, reject ){ // Reverse Geocoding geocoder.reverseGeocode(location.latitude,location.longitude,(err,data) => { if (err) {console.log(err)reject( err ); } else {// console.log(’geocoded: ’ + JSON.stringify(data))console.log(’geocoded: ’ + JSON.stringify(data.results[0].formatted_address))conv.ask(new SimpleResponse({ speech:’You currently at ’ + data.results[0].formatted_address + ’. What would you like to do Now?’, text: ’You currently at ’ + data.results[0].formatted_address + ’.’}))conv.ask(new Suggestions([’Back to Menu’, ’Learn More’, ’Quit’]))resolve() } }) });}function userInfo ( conv, params, granted) { if (conv.arguments.get(’PERMISSION’)) { return userInfoFound( conv, params, granted ); } else { return userInfoNotFound( conv, params, granted ); }}解决方法
我正在使用 Dialogflow* , Cloud Functions 和新的 NodeJS Client Library V2for Google on Actions 为Google Home 构建一个 助手应用程序。实际上,我正在将用V1构建的旧代码迁移到V2。 ***
上下文
我正在尝试使用两个单独的意图来获取用户的位置:(Request Permission触发/向用户发送许可请求的UserInfo意图)和(检查用户是否已授予许可,然后返回助手请求继续的数据的意图。
问题
问题在于,在V1上正常工作的同一代码在V2上无效。所以我不得不做一些重构。当我部署云功能时,我能够成功请求用户的许可,获取他的位置,然后使用外部库(geocode),我可以将latlong转换为人类可读的形式。但是由于某些原因(我认为它的承诺),我无法解析承诺对象并将其显示给用户
错误
我收到以下错误:
代码
以下是我的Cloud功能代码。我使用request库,https库等尝试了此代码的多个版本。不走运…不走运
const {dialogflow,Suggestions,SimpleResponse,Permission} = require(’actions-on-google’) const functions = require(’firebase-functions’); const geocoder = require(’geocoder’); const app = dialogflow({ debug: true }); app.middleware((conv) => {conv.hasScreen = conv.surface.capabilities.has(’actions.capability.SCREEN_OUTPUT’);conv.hasAudioPlayback = conv.surface.capabilities.has(’actions.capability.AUDIO_OUTPUT’); }); function requestPermission(conv) {conv.ask(new Permission({ context: ’To know who and where you are’,permissions: [’NAME’,’DEVICE_PRECISE_LOCATION’]})); } function userInfo ( conv,params,granted) {if (!conv.arguments.get(’PERMISSION’)) { // Note: Currently,precise locaton only returns lat/lng coordinates on phones and lat/lng coordinates // and a geocoded address on voice-activated speakers. // Coarse location only works on voice-activated speakers. conv.ask(new SimpleResponse({speech:’Sorry,I could not find you’,text: ’Sorry,I could not find you’ })) conv.ask(new Suggestions([’Locate Me’,’Back to Menu’,’ Quit’]))}if (conv.arguments.get(’PERMISSION’)) { const permission = conv.arguments.get(’PERMISSION’); // also retrievable with explicit arguments.get console.log(’User: ’ + conv.user) console.log(’PERMISSION: ’ + permission) const location = conv.device.location.coordinates console.log(’Location ’ + JSON.stringify(location)) // Reverse Geocoding geocoder.reverseGeocode(location.latitude,location.longitude,(err,data) => {if (err) { console.log(err)}// console.log(’geocoded: ’ + JSON.stringify(data))console.log(’geocoded: ’ + JSON.stringify(data.results[0].formatted_address))conv.ask(new SimpleResponse({ speech:’You currently at ’ + data.results[0].formatted_address + ’. What would you like to do now?’,text: ’You currently at ’ + data.results[0].formatted_address + ’.’}))conv.ask(new Suggestions([’Back to Menu’,’Learn More’,’Quit’])) })} } app.intent(’Request Permission’,requestPermission); app.intent(’User Info’,userInfo); exports.myCloudFunction = functions.https.onRequest(app);
很感谢任何形式的帮助。谢谢