veenaMSL
veenaMSL

Reputation: 111

'TypeError: Cannot initialize connector undefined: Cannot read property 'root' of undefined'

I'm using loopback3.x. I want to integrate 3rd party APIs with loopback. For that while using loopback-connector-rest shows the error 'TypeError: Cannot initialize connector undefined: Cannot read property 'root' of undefined'. How to fix it?

Datasource configuration

   "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"]
      }
    }]
  }

Error

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)

Upvotes: 1

Views: 2090

Answers (1)

Miroslav Bajtoš
Miroslav Bajtoš

Reputation: 10785

In the functions section, you have defined a function notify accepting several input arguments: title, text, click_action, keyname, to. However, the template section does not provide any information on how to map those arguments to an HTTP request. For example, is the title supposed to be sent via URL query or in the request body?

IIUC, you are trying to use the legacy Firbase Cloud Messaging HTTP API as described here: https://firebase.google.com/docs/cloud-messaging/http-server-ref Based on that documentation, I think all arguments of your function should be sent in the request body.

It looks like LoopBack's REST connector is not correctly detecting and handling the situation when an input argument is not mapped to any HTTP source. It should not be crashing, feel free to open a bug report in https://github.com/strongloop/loopback-connector-rest/issues

Here is a configuration that does not crash the server. I don't have a Firebase account to verify that it's working as expected.

    "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}"
      }
    },

You can learn more about different ways how to configure input arguments in the connector documentation: https://loopback.io/doc/en/lb3/REST-connector.html#defining-a-custom-method-using-a-template

Upvotes: 5

Related Questions