我正在使用 loopback3.x 。我想将第三方API与回送集成。为此,在使用loopback-connector-rest时显示错误' TypeError:无法初始化未定义的连接器:无法读取未定义的属性'root''。如何解决?
数据源配置
"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)
答案 0 :(得分:3)
在functions
部分中,您定义了一个函数notify
,该函数接受多个输入参数:title
,text
,click_action
,keyname
,to
。但是,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