首页 > 解决方案 > 无法使用节点 js 在谷歌对话流中创建自定义实体

问题描述

下面出现错误

(节点:14284)UnhandledPromiseRejectionWarning:错误:3 INVALID_ARGUMENT:com.google.apps.framework.request.BadRequestException:必须提供要添加的实体。在 Object.callErrorFromStatus (E:\workspace\dailogFlowApi\node_modules@grpc\grpc-js\build\src\call.js:31:26) 在 Object.onReceiveStatus (E:\workspace\dailogFlowApi\node_modules@grpc\grpc- js\build\src\client.js:179:52) 在 Object.onReceiveStatus (E:\workspace\dailogFlowApi\node_modules@grpc\grpc-js\build\src\client-interceptors.js:336:141) 在 Object .onReceiveStatus (E:\workspace\dailogFlowApi\node_modules@grpc\grpc-js\build\src\client-interceptors.js:299:181) 在 E:\workspace\dailogFlowApi\node_modules@grpc\grpc-js\build\ src\call-stream.js:145:78 在 processTicksAndRejections (internal/process/task_queues.js:79:11) (node:14284) UnhandledPromiseRejectionWarning: 未处理的承诺拒绝。此错误源于在没有 catch 块的情况下抛出异步函数内部,或拒绝未使用 .catch() 处理的承诺。要在未处理的 Promise 拒绝时终止节点进程,请使用 CLI 标志--unhandled-rejections=strict(见https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode)。(拒绝 id:1)(节点:14284)[DEP0018] DeprecationWarning:不推荐使用未处理的承诺拒绝。将来,未处理的 Promise 拒绝将使用非零退出代码终止 Node.js 进程。


exports.createEntityTypeService = (entity) => {


  return new Promise(async (resolve, reject) => {
    try {
      let projectPath = entityClient.agentPath(projectId);
      let entityPath = entityClient.entityTypePath(projectId, entity);
      let entityValues = ["testEntity1","test", "testEntity2"];
      const entities = [];
      entityValues.forEach((entityValue) => {
        
        entities.push({
          value: entityValue,
          synonyms: [entityValue],
        });
      });

      let entityReuest = {
        parent: projectPath,
        languageCode:'en-US',
        enitityType: {
          name: entityPath,
          entityOverrideMode:"@",
          entities: entities,
        },
        
      };
      let response = await entityClient.createEntityType(entityReuest);
      console.log(response);
      resolve(response);
    } catch (error) {
      reject(error);
    }
  });
};```

标签: node.jsdialogflow-es

解决方案


您正在使用createEntityType()并且为entityType. 因此,您遇到 INVALID_ARGUMENT 的错误。

在此处输入图像描述

根据文档,entityType的参数如下:

  • autoExpansionMode - EntityType 自动扩展模式。

  • displayName - 实体类型显示名称。

  • enableFuzzyExtraction - EntityType 启用模糊提取。

  • 实体- EntityType 实体。

  • kind - EntityType 种类。

  • name - 实体类型名称。

在您的代码中entityType应该看起来像这样。只需确保为每个参数提供正确的值。

enitityType: {
  name: entityPath,
  entities: entities,
  autoExpansionMode: autoExpansionMode,
  displayName: displayName
  enableFuzzyExtraction: enableFuzzyExtraction
  kind: kind
}

推荐阅读