传递上下文时无法读取未定义的 属性 'parameters' - Google 上的操作

Cannot read property 'parameters' of undefined while passing Context - Actions on Google

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

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

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

Cannot read property 'parameters' of undefined.

我正在使用 dialog v2 api 和 Google sdk 上的操作。下面的 link 提供了一个如何实现上下文的示例,我看不出有什么大的不同。 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']

let index = movieCtx.parameters[index]

应该是

let index = movieCtx.parameters['index']

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

我找到了答案!

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