NodeJs DialogFlow:名称''与模式不匹配

时间:2018-11-28 22:18:06

标签: node.js heroku dialogflow

我正在尝试使用Dialogflow NodeJS API创建会话实体类型。到目前为止,我的代码如下:

app.post('/addsession', (req, res) => {
    pathJoiner = require("path");
    process.env.GOOGLE_APPLICATION_CREDENTIALS = pathJoiner.join(__dirname, "/config/AgentKeyFile.json");
    createSessionEntityType(req.body.path, res);
});

function createSessionEntityType(sessionPath, res) {

  const dialogflow = require('dialogflow');

  // Instantiates clients
  const sessionEntityTypesClient = new dialogflow.SessionEntityTypesClient();

  const entitiesArr = [{
      "value": "Test Name",
      "synonyms": ["Test Name", "Test"]
  }];

  const createSessionEntityTypeRequest = {
      parent: sessionPath,
      session_entity_type: {
          name: sessionPath + "/entityTypes/Friends-Name",
          entity_override_mode: "ENTITY_OVERRIDE_MODE_OVERRIDE",
          entities: entitiesArr
      },
  };

  sessionEntityTypesClient
   .createSessionEntityType(createSessionEntityTypeRequest)
   .then(responses => {
      console.log("Entity type created: " + responses);
      res.setHeader('Content-Type', 'application/json');
      res.send(JSON.stringify(responses.body));
   })
}

但是,当我在Heroku服务器上运行此代码时,出现以下错误:

UnhandledPromiseRejectionWarning: Error: 3 INVALID_ARGUMENT: Name '' does not match patterns
'projects/{projectId=*}/agent/environments/{environmentId=*}/users/{userId=*}
/sessions/{sessionId=*}/entityTypes/{entityTypeName=*},projects/
{projectId=*}/agent/sessions/{sessionId=*}/entityTypes/{entityTypeName=*}'

我不确定为什么总是说name参数为空。我知道我丢失了一些东西,但无法弄清楚是什么。

1 个答案:

答案 0 :(得分:0)

我将代码更改为以下内容,问题消失了

   const dialogflow = require('dialogflow');
   const projectId = "projId";

   // Instantiates clients
  const sessionEntityTypesClient = new dialogflow.SessionEntityTypesClient();
  const sessionPath = sessionEntityTypesClient.sessionPath(projectId, sessionId);
  const sessionEntityTypePath = sessionEntityTypesClient.sessionEntityTypePath(projectId, sessionId, "Entity-Name");

  const entitiesArr = [{
       "value": "Test Name",
       "synonyms": ["Test Name", "Test"]
  }];

  const createSessionEntityTypeRequest = {
      parent: sessionPath,
      sessionEntityType: {
         name: sessionEntityTypePath,
         entityOverrideMode: "ENTITY_OVERRIDE_MODE_OVERRIDE",
         entities: entitiesArr
      },
  };

  sessionEntityTypesClient
     .createSessionEntityType(createSessionEntityTypeRequest)
     .then(responses => {
     console.log("Entity type created: " + responses);
     res.setHeader('Content-Type', 'application/json');
     res.send(JSON.stringify(responses[0]));
  })