2017-08-03 238 views
1

我使用api.ai在node.js客户端库下面进行测试,我复制了本页给出示例的确切代码。ApiAiApp不是构造函数

https://developers.google.com/actions/reference/nodejs/ApiAiApp

但每次我得到

TypeError: ApiAiApp is not a constructor 
at /app/index.js:9:15 
at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5) 

这是我的index.js文件:

var express = require('express'); 
var request = require('request'); 
var exps = express(); 

const ApiAiApp = require('actions-on-google').ApiAiApp; 

exps.post('/hook', function(request, response) { 

    const app = new ApiAiApp({request, response}); 

    response.status(200) 
    response.json({ 
     speech: "Hello from hook", 
     displayText: "Hello from hook", 
     source: 'HOOK' 
    }) 
    console.log("RESPONSE :::: \n"); 
    console.log(response); 
}); 

exps.listen((process.env.PORT || 8000), function() { 
    console.log("App up and running, listening.") 
}) 

的package.json

{ 
    "name": "googleActionNode", 
    "version": "1.0.0", 
    "description": "", 
    "main": "index.js", 
    "scripts": { 
    "start": "node index.js" 
    }, 
    "author": "Sukhvinder Singh", 
    "dependencies": { 
    "actions-on-google": "1.2.0", 
    "body-parser": "^1.15.0", 
    "express": "^4.13.4" 
    }, 
    "devDependencies": {}, 
    "repository": { 
    "type": "git", 
    "url": "git+https://github.com/sukhvinder1/googleActionNode.git" 
    }, 
    "license": "ISC", 
    "bugs": { 
    "url": "https://github.com/sukhvinder1/googleActionNode/issues" 
    }, 
    "homepage": "https://github.com/sukhvinder1/googleActionNode#readme" 
} 

如果我commnent出在线以下,一切工作正常。

const app = new ApiAiApp({request: request, response: response}); 

任何人都可以帮我吗?

+0

就在你尝试调用new之前,看看你是否可以做一个console.log(ApiAiApp);和console.log(typeof ApiAiApp); – Prisoner

+0

同时发布您的package.json以及您正在使用的谷歌操作版本 – Prisoner

+0

由于@Prisoner将我指向了正确的文档,我删除了我的答案。我会根据他的想法来了解如何进行调试以使其工作。 –

回答

1

您收到此错误是因为您正在调用的方法不是针对最初使用的版本的Google操作的一部分。

在1.0版本中,你会以这种方式称之为:

const Assistant = require('actions-on-google').ApiAiAssistant; 

后来的版本改变了这种以

const Assistant = require('actions-on-google').ApiAiApp; 

后来的版本仍然允许早期的名字,但它已被弃用,您应该切换到新名称和最新的库版本。

顺便说一句,你可能想看看我写的这个例子:https://github.com/greenido/bitcoin-info-action/blob/master/index.js 它显示了我们希望与谷歌操作(例如使用地图连接动作到意图)的方式。

我希望它有帮助。

0

该文件中有一堆东西让我感到困惑,我不知道它是否也令人困惑的node.js。

  • 你不应该需要创建两个ActionsSdkApp和​​的定义。我会尝试删除ActionsSdkApp一个,看看是否有效。

  • 您不应该致电status()json()(对于res对象,我认为该对象不存在于此版本的文件中)。

  • 行号不匹配,但我会假设有其他的事情可能会混淆这个问题。

+0

感谢您的回复,我刚刚编辑了我的问题。我希望现在不是很混乱。 但是,我仍然得到相同的错误。 – Sukh