首页 > 解决方案 > 类型错误:无法初始化未定义的连接器:无法在 loopback4 中读取未定义的属性“根”。如何解决?

问题描述

我正在使用环回4。我想将 3rd 方 API 与环回集成。为此,在使用 loopback-connector-rest 时显示错误“TypeError:无法初始化未定义的连接器:无法读取未定义的属性“根”。如何解决?

我有一个如下所示的数据源:

const config = {
  name: "authds",
  connector: "rest",
  baseURL: "http://xxxxxxx.xxxxx.com:3000",
  crud: false,
  options: {
    headers: {
      accept: "application/json",
      contentType: "application/json"
    }
  },
  operations: [
    {
      template: {
        method: "POST",
        url: "http://xxxxxxx.xxxxx.com:3000/users/signup",
        body: {
          password: "{password: string}",
          login: "{login: string}",
          email: "{email: string}",
          userType: "{userType: string}",
          agencyId: "{agencyId: string}",
          photo: "{photo: string}",
          entityId: "{entityId: string}",
          createdBy: "{createdBy: string}"
        }
      },

      functions: {
        createUser: [
          "password",
          "login",
          "email",
          "userType",
          "agencyId",
          "photo",
          "entityId",
          "createdBy"
        ]
      }
    }
  ]
};

在我的 userService.ts 文件中,我有这样的内容:

import {inject, Provider} from '@loopback/core';
import {getService} from '@loopback/service-proxy';
import {AuthdsDataSource} from '../datasources';

export interface UserData {
    password: string,
    login: any,
    email: any,
    userType: any,
    agencyId: any,
    photo: any,
    entityId: any,
    createdBy: any
}
export interface UserService {
  // this is where you define the Node.js methods that will be
  // mapped to REST/SOAP/gRPC operations as stated in the datasource
  // json file.
    createUser(user: UserData): Promise<object>;
}

export class UserServiceProvider implements Provider<UserService> {
  constructor(
    // authds must match the name property in the datasource json file
    @inject('datasources.authds')
    protected dataSource: AuthdsDataSource = new AuthdsDataSource(),
  ) {}

  value(): Promise<UserService> {
    return getService(this.dataSource);
  }
}

它给出了这样的错误:

Cannot start the application. TypeError: Cannot initialize connector "rest": Cannot read property 'root' of undefined
    at C:\nodejs\clientservice\node_modules\loopback-connector-rest\lib\rest-connector.js:93:28
    at Array.forEach (<anonymous>)
    at C:\nodejs\clientservice\node_modules\loopback-connector-rest\lib\rest-connector.js:87:22
    at Array.forEach (<anonymous>)
    at Function.initializeDataSource [as initialize] (C:\nodejs\clientservice\node_modules\loopback-connector-rest\lib\rest-connector.js:52:25)
    at AuthdsDataSource.DataSource.setup (C:\nodejs\clientservice\node_modules\loopback-datasource-juggler\lib\datasource.js:515:19)
    at new DataSource (C:\nodejs\clientservice\node_modules\loopback-datasource-juggler\lib\datasource.js:147:8)
    at new AuthdsDataSource (C:\nodejs\clientservice\src\datasources\authds.datasource.ts:63:5)
    at C:\nodejs\clientservice\node_modules\@loopback\context\src\resolver.ts:76:14
    at Object.transformValueOrPromise (C:\nodejs\clientservice\node_modules\@loopback\context\src\value-promise.ts:298:12)
    at Object.instantiateClass (C:\nodejs\clientservice\node_modules\@loopback\context\src\resolver.ts:69:35)
    at C:\nodejs\clientservice\node_modules\@loopback\context\src\binding.ts:846:21
    at Binding._getValue (C:\nodejs\clientservice\node_modules\@loopback\context\src\binding.ts:684:14)
    at C:\nodejs\clientservice\node_modules\@loopback\context\src\binding.ts:530:23
    at C:\nodejs\clientservice\node_modules\@loopback\context\src\resolution-session.ts:122:13
    at tryCatchFinally (C:\nodejs\clientservice\node_modules\@loopback\context\src\value-promise.ts:222:14)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! clientservice@0.0.1 start: `node -r source-map-support/register .`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the clientservice@0.0.1 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.


标签: loopback4

解决方案


我通过删除函数中的 createUser 字段解决了这个问题。

谢谢 !


推荐阅读