【问题标题】:'TypeError: Cannot initialize connector undefined: Cannot read property 'root' of undefined''TypeError:无法初始化未定义的连接器:无法读取未定义的属性'root''
【发布时间】:2020-01-06 10:27:20
【问题描述】:

我正在使用 loopback3.x。我想将 3rd 方 API 与环回集成。为此,在使用 loopback-connector-rest 时会显示错误“TypeError: Cannot initialize connector undefined: Cannot read property 'root' of undefined”。如何解决?

数据源配置

   "restDataSource": {
    "name": "restDataSource",
    "baseURL": "",
    "crud": true,
    "connector": "rest",
    "debug": false,
    "options": {
      "headers": {
        "accept": "application/json",
        "content-type": "application/json",
        "authorization": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
      },
      "strictSSL": false
    },
    "operations": [{
      "template": {
        "method": "POST",
        "url": "https://fcm.googleapis.com/fcm/send",
        "options": {
          "strictSSL": true,
          "useQuerystring": true
        }
      },
      "functions": {
        "notify": ["title", "text", "click_action", "keyname", "to"]
      }
    }]
  }

错误

TypeError: Cannot create data source "restDataSource": Cannot initialize connector "rest": Cannot read property 'root' of undefined
    at /home/veena-msl/Documents/care-doc-api/node_modules/loopback-connector-rest/lib/rest-connector.js:93:28
    at Array.forEach (<anonymous>)
    at /home/veena-msl/Documents/care-doc-api/node_modules/loopback-connector-rest/lib/rest-connector.js:87:22
    at Array.forEach (<anonymous>)
    at Function.initializeDataSource [as initialize] (/home/veena-msl/Documents/care-doc-api/node_modules/loopback-connector-rest/lib/rest-connector.js:52:25)
    at DataSource.setup (/home/veena-msl/Documents/care-doc-api/node_modules/loopback-datasource-juggler/lib/datasource.js:493:19)
    at new DataSource (/home/veena-msl/Documents/care-doc-api/node_modules/loopback-datasource-juggler/lib/datasource.js:138:8)
    at Registry.createDataSource (/home/veena-msl/Documents/care-doc-api/node_modules/loopback/lib/registry.js:364:12)
    at dataSourcesFromConfig (/home/veena-msl/Documents/care-doc-api/node_modules/loopback/lib/application.js:570:19)
    at Function.app.dataSource (/home/veena-msl/Documents/care-doc-api/node_modules/loopback/lib/application.js:269:14)
    at /home/veena-msl/Documents/care-doc-api/node_modules/loopback-boot/lib/executor.js:191:9
    at /home/veena-msl/Documents/care-doc-api/node_modules/loopback-boot/lib/executor.js:282:5
    at Array.forEach (<anonymous>)
    at forEachKeyedObject (/home/veena-msl/Documents/care-doc-api/node_modules/loopback-boot/lib/executor.js:281:20)
    at setupDataSources (/home/veena-msl/Documents/care-doc-api/node_modules/loopback-boot/lib/executor.js:181:3)
    at execute (/home/veena-msl/Documents/care-doc-api/node_modules/loopback-boot/lib/executor.js:39:3)

【问题讨论】:

  • 能否请您发布 REST 连接器的数据源配置?只要确保删除任何敏感信息,如密码等。
  • @MiroslavBajtoš 我发布了数据源配置。你能检查一下吗?
  • 谢谢。我在您的数据源配置中看不到任何明显的问题。您能否发布您收到的错误的完整堆栈跟踪?
  • @MiroslavBajtoš 发布了完整的堆栈跟踪错误。

标签: loopbackjs connector


【解决方案1】:

functions 部分中,您定义了一个函数notify 接受多个输入参数:titletextclick_actionkeynameto。但是,template 部分没有提供有关如何将这些参数映射到 HTTP 请求的任何信息。例如,title 应该通过 URL 查询发送还是在请求正文中发送?

IIUC,您正在尝试使用此处所述的旧版 Firbase Cloud Messaging HTTP API:https://firebase.google.com/docs/cloud-messaging/http-server-ref 根据该文档,我认为您的函数的所有参数都应在请求正文中发送。

当输入参数未映射到任何 HTTP 源时,LoopBack 的 REST 连接器似乎没有正确检测和处理这种情况。它应该不会崩溃,请随时在https://github.com/strongloop/loopback-connector-rest/issues 中打开错误报告

这是一个不会使服务器崩溃的配置。我没有 Firebase 帐户来验证它是否按预期工作。

    "template": {
      "method": "POST",
      "url": "https://fcm.googleapis.com/fcm/send",
      "options": {
        "strictSSL": true,
        "useQuerystring": true
      },
      "body": {
        "title": "{title:string}",
        "text": "{text:string}",
        "click_action": "{click_action:string}",
        "keyname": "{keyname:string}",
        "to": "{to:string}"
      }
    },

您可以在连接器文档中了解有关如何配置输入参数的不同方法的更多信息:https://loopback.io/doc/en/lb3/REST-connector.html#defining-a-custom-method-using-a-template

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-29
    • 1970-01-01
    • 2021-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多