【问题标题】:Cannot read property 'parameters' of undefined while passing Context - Actions on Google传递上下文时无法读取未定义的属性“参数” - Google 上的操作
【发布时间】:2018-12-10 17:00:42
【问题描述】:

目前我正在尝试开发一个动作。

我正在努力实现重用意图。具体来说,就是说助理给我一个清单的前三项,如果我要求更多,那么它应该给我清单的后三项。

我实现了在本地工作的逻辑,但遗憾的是我总是从列表中获得前三个项目。因此,我尝试使用上下文,我想将索引作为参数传递,在这里我遇到了问题。云功能日志总是给我:

无法读取未定义的属性“参数”。

我正在使用 dialog v2 api 和 Google sdk 上的操作。以下链接提供了一个如何实现上下文的示例,我认为那里没有太大区别。 https://developers.google.com/actions/reference/nodejsv2/overview

为其编写的代码如下所示:

app.intent('Default Welcome Intent', conv => {
    conv.ask('Welcome.')

    const lifespan = 100;
    const parameters = {
        index: 0,
    };
    conv.contexts.set('movieCtx', lifespan, parameters);
});

app.intent('New Cinema Movies Intent', async conv => {
    let movieCtx = conv.contexts.get('movieCtx');
    let index = movieCtx.parameters[index]

    let movieRepository = new MovieRepository();
    movieRepository.index = index;
    await movieRepository.fetchNextTitle(3).then(function(titles) {
        movieCtx.parameters[index] = movieRepository.index;
        conv.ask('The 3 movies are:' + titles.join(', '))

        return true;
    }).catch(function(err) {
        console.log(err); 
        return false;
    });
})

我的依赖:

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "scripts": {
    "lint": "eslint .",
    "serve": "firebase serve --only functions",
    "shell": "firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "dependencies": {
    "actions-on-google": "^2.5.0",
    "firebase-admin": "~6.0.0",
    "firebase-functions": "^2.0.3",
    "pg": "^7.7.1"
  },
  "devDependencies": {
    "eslint": "^4.12.0",
    "eslint-plugin-promise": "^3.6.0"
  },
  "private": true
}

更新:

我也尝试更改以下几行,但也无济于事:

const parameters = {
        movieIndex: 0,
};

let index = movieCtx.parameters['movieIndex']

【问题讨论】:

  • 应该是 conv.contexts.get('movieCtx')
  • 谢谢!这实际上是真的,我纠正了它,但仍然无法正常工作:(
  • 这一行也让 index = movieCtx.parameters.index
  • 我也试过了,但仍然遇到同样的错误。如果我正在模拟该操作,我会收到以下错误:由于语音响应为空,无法将 Dialogflow 响应解析为 AppResponse。云函数给我参数错误。

标签: dialogflow-es actions-on-google


【解决方案1】:

线

let index = movieCtx.parameters[index]

应该是

let index = movieCtx.parameters['index']

(您之前没有定义index,所以它试图获取未定义的引用,这是无效的。)

【讨论】:

  • 嘿囚犯,谢谢你的回答!可悲的是,这对我不起作用。我还尝试执行以下操作:const parameters = { movieIndex: 0, }; 然后将行更改为:let index = movieCtx.parameters['movieIndex']; 这对我也没有帮助。
  • 请不要在 cmets 中显示代码。如果您更改了代码,请将其添加到原始问题中。
【解决方案2】:

我找到了答案!

const {dialogflow} = require('actions-on-google');
const functions = require('firebase-functions');
const MovieRepository = require('./PostgreSQL/MovieRepository');
let movieRepository = new MovieRepository();

const app = dialogflow({debug: true});

const AppContexts = {
    NUMBER: 'number',
}

app.intent('Default Welcome Intent', (conv) => {
    const lifespan = 10;
    const parameters = {
        movieIndex: 0,
    };
    conv.contexts.set(AppContexts.NUMBER, lifespan, parameters);
    conv.ask('Welcome!')
});

app.intent('New Cinema Movies Intent', async (conv) => {
    const movieCtx = conv.contexts.get(AppContexts.NUMBER);

    await movieRepository.fetchNextTitle(3).then(function(titles) {
        movieCtx.parameters.movieIndex = movieRepository.index;
        conv.ask('The 3 movie titles are: ' + titles.join(', '));
        return true;
    }).catch(function(err) {
        console.log(err); 
        return false;
    });
})

app.intent('Default Fallback Intent', conv => {
    conv.ask('Sorry. Can you repeat?')
})

exports.dialogflowFirebaseFulfillment = functions.https.onRequest(app);

如您所见,您必须在意图之上创建 AppContext。有点奇怪但重要的是你申请了

NUMBER: 'number'

到上下文,否则你会再次得到一个未定义的上下文。要访问您的参数,您只需像这样附加您的参数值:

movieCtx.parameters.movieIndex

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-01
    • 2019-01-29
    • 2019-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多