Thursday, May 20, 2021

"cannot read property prototype of undefined" lb4 repository decorator

 Using Loopback4, I was trying to create new repositories and noticed afterwards when trying to start the app, I had the following error:

 TypeError: Cannot read property 'prototype' of undefined  
   at Object.repository (C:\sts-workspace\rodentity-nodejs\node_modules\@loopback\repository\src\decorators\repository.decorator.ts:145:53)  
   at Object.<anonymous> (C:\sts-workspace\rodentity-nodejs\src\services\unfinished-genotyping-plates.service.ts:17:6)  
   at Module._compile (internal/modules/cjs/loader.js:778:30)  
   at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)  
   at Module.load (internal/modules/cjs/loader.js:653:32)  
   at tryModuleLoad (internal/modules/cjs/loader.js:593:12)  
   at Function.Module._load (internal/modules/cjs/loader.js:585:3)  
   at Module.require (internal/modules/cjs/loader.js:692:17)  
   at require (internal/modules/cjs/helpers.js:25:18)  
   at Object.<anonymous> (C:\sts-workspace\rodentity-nodejs\src\services\index.ts:3:1)  
   at Module._compile (internal/modules/cjs/loader.js:778:30)  
   at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)  
   at Module.load (internal/modules/cjs/loader.js:653:32)  
   at tryModuleLoad (internal/modules/cjs/loader.js:593:12)  
   at Function.Module._load (internal/modules/cjs/loader.js:585:3)  
   at Module.require (internal/modules/cjs/loader.js:692:17)  

The error message is not very helpful. But after debugging it, I worked I had a missed a step when creating the repository. I needed to add a line to the application.ts file to register the newly created repositories as follows:

 this.repository(MouseRepository);