Use the lb4 repository cmd from the cli
When I run this comand I got this error :
$ lb4 repository
(node:1932) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'kind' of undefined
at Object.exports.getDataSourceConfig (C:\Users\vanvan\AppData\Roaming\npm\node_modules\@loopback\cli\lib\ast-helper.js:161:58)
at readDataSourceConfigFromTypeScript (C:\Users\vanvan\AppData\Roaming\npm\node_modules\@loopback\cli\lib\utils.js:630:18)
at Object.getDataSourceConfig (C:\Users\vanvan\AppData\Roaming\npm\node_modules\@loopback\cli\lib\utils.js:608:5)
at Object.exports.isConnectorOfType (C:\Users\vanvan\AppData\Roaming\npm\node_modules\@loopback\cli\lib\utils.js:583:26)
at datasourcesList.filter.item (C:\Users\vanvan\AppData\Roaming\npm\node_modules\@loopback\cli\generators\repository\index.js:264:28)
at Array.filter (<anonymous>)
at RepositoryGenerator.promptDataSourceName (C:\Users\vanvan\AppData\Roaming\npm\node_modules\@loopback\cli\generators\repository\index.js:263:50)
(node:1932) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:1932) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
I would like to be able to use the generator of repository.
win32 x64 10.16.2
+-- @loopback/[email protected]
+-- @loopback/[email protected]
+-- @loopback/[email protected]
+-- @loopback/[email protected]
+-- @loopback/[email protected]
+-- @loopback/[email protected]
+-- @loopback/[email protected]
+-- @loopback/[email protected]
+-- @loopback/[email protected]
+-- @loopback/[email protected]
+-- [email protected]
Hey @ybarbaria I don't think this is a bug in loopback you please try creating your app again and please update your typescript version as well as update @loopback/cli to the latest version.
Hi @ybarbaria, do try @HrithikMittal's suggestion of updating @loopback/cli to the latest version.
If that doesn't work, could you please add a link to a reproduction sandbox GitHub repo? If not, could you provide a step-by-step guide on how to replicate the issue from a blank slate?
This would help a lot with understanding the structure of the LoopBack 4 app, which seems to be the issue.
Hi @HrithikMittal @achrinza , @loopback/cli is already on the last version 2.9.0. I am going to try to reproduce the bug on a blank project if I can't reproduce I will try to give you more information about my project. Thanks for your help guys. Same for typescript last version 3.9.5.
$ lb4 -v
@loopback/cli version: 2.9.0
@loopback/* dependencies:
- @loopback/authentication: ^4.2.7
- @loopback/boot: ^2.3.3
- @loopback/build: ^5.4.3
- @loopback/context: ^3.9.0
- @loopback/core: ^2.8.0
- @loopback/metadata: ^2.2.0
- @loopback/openapi-spec-builder: ^2.1.7
- @loopback/openapi-v3: ^3.4.3
- @loopback/repository-json-schema: ^2.4.4
- @loopback/repository: ^2.7.0
- @loopback/rest: ^5.1.1
- @loopback/testlab: ^3.1.7
- @loopback/docs: ^3.9.0
- @loopback/example-hello-world: ^2.1.3
- @loopback/example-log-extension: ^2.1.3
- @loopback/example-rpc-server: ^2.1.3
- @loopback/example-todo: ^3.5.1
- @loopback/example-soap-calculator: ^2.2.3
- @loopback/service-proxy: ^2.3.2
- @loopback/http-caching-proxy: ^2.1.7
- @loopback/http-server: ^2.1.7
- @loopback/example-todo-list: ^3.3.1
- @loopback/dist-util: ^0.4.0
- @loopback/rest-explorer: ^2.2.4
- @loopback/eslint-config: ^8.0.1
- @loopback/example-express-composition: ^2.2.3
- @loopback/example-greeter-extension: ^2.1.3
- @loopback/booter-lb3app: ^2.2.3
- @loopback/example-lb3-application: ^2.2.1
- @loopback/example-greeting-app: ^2.2.1
- @loopback/example-context: ^2.1.3
- @loopback/repository-tests: ^0.12.7
- @loopback/extension-health: ^0.4.7
- @loopback/authorization: ^0.6.0
- @loopback/rest-crud: ^0.8.7
- @loopback/security: ^0.2.12
- @loopback/authentication-passport: ^2.1.7
- @loopback/example-metrics-prometheus: ^0.3.3
- @loopback/extension-metrics: ^0.3.7
- @loopback/model-api-builder: ^2.1.7
- @loopback/extension-logging: ^0.3.7
- @loopback/example-access-control-migration: ^2.0.0
- @loopback/example-file-transfer: ^1.3.3
- @loopback/example-rest-crud: ^1.4.1
- @loopback/apiconnect: ^0.3.5
- @loopback/example-validation-app: ^1.5.1
- @loopback/cron: ^0.2.7
- @loopback/example-multi-tenancy: ^0.4.1
- @loopback/example-passport-login: ^1.4.3
- @loopback/authentication-jwt: ^0.4.1
- @loopback/context-explorer: ^0.1.7
- @loopback/express: ^1.2.3
- @loopback/example-todo-jwt: ^1.1.1
- @loopback/mock-oauth2-provider: ^0.1.0
Hello,
So I've started a new blank project and lb4 repository works, so after investigation looks like there is something into my datasource file which lb4 doesn't like but I don't know where came the error :(
import {inject, lifeCycleObserver, LifeCycleObserver} from '@loopback/core';
import {juggler} from '@loopback/repository';
import * as devConfig from './dev.datasource.json'; // import dev json
import * as prodConfig from './prod.datasource.json'; // import prod json
export class DataSourceSettings {
name: string;
connector: string;
url?: string;
host?: string;
port: number;
user: string;
password: string;
database: string;
connectionLimit?: number;
connectTimeout?: number;
acquireTimeout?: number;
timeout?: number;
}
let config: DataSourceSettings;
if (process.env.NODE_ENV === 'production') {
console.log(' --- Database in Prod Mode --- ');
config = prodConfig;
console.log(config);
} else {
console.log(' --- Database in Dev Mode --- ');
config = devConfig;
console.log(config);
}
@lifeCycleObserver('datasource')
export class DbDataSource extends juggler.DataSource
implements LifeCycleObserver {
static dataSourceName = 'db';
constructor(
@inject('ds.config.db', {optional: true}) dsConfig: object = config,
) {
super(dsConfig);
}
}
Hey @ybarbaria This is your connection file and in class DataSourceSetting Please give details of your database into this and try running it again.
npm run build
npm run migrate
npm start
Hey @HrithikMittal,
I know it's my connection file I've created it :)
What kind of details do you want regarding my db ? I use the loopback mysql connector
I have already try the cmd you said nothing change.
this is my datasource,json
{
"name": "db",
"connector": "mysql",
"url": "",
"host": "127.0.0.1",
"port": 3306,
"user": "***",
"password": "****",
"database": "****",
"connectionLimit": 1000,
"connectTimeout": 300000,
"acquireTimeout": 300000,
"timeout": 300000
}
Everything works I can get/set data from my db, I can migrate (alter/drop) from the migrate.ts file.
I've this error only when I use
lb4 repository
lb4 controller works, I can run my project without any trouble.
Can you please share the repo.
Github link of your repository.
@HrithikMittal I am sorry I can't. This project belong to a company I can't give access to the repo to someone else :( .
I think it has something to do with the extra configuration class (DataSourceSettings). I have the same setup and error as you (I think we used the same tutorial somewhere).
It is still possible to manually create the repository, but it would be nice to be able to do it using the CLI.
I created a test repo for this issue: https://github.com/juleskreutzer/lb4-repository-test
When you run the command lb4 repository, you will get the error @ybarbaria mentioned.
I think it has something to do with the extra configuration class (
DataSourceSettings). I have the same setup and error as you (I think we used the same tutorial somewhere).It is still possible to manually create the repository, but it would be nice to be able to do it using the CLI.
I created a test repo for this issue: https://github.com/juleskreutzer/lb4-repository-test
When you run the commandlb4 repository, you will get the error @ybarbaria mentioned.
I think so,when I use a blank project with the standard datasource settings I don't have this issue, thanks for the test repo :) I wanted to do the same but no time :)
Yeah currently I have created my repository manually it's not hard but would be easier with the CLI.
Hey @ybarbaria if you issue is solved then you can close this issue. Thanks
Hey @HrithikMittal is not fix, this issue is related to lb4 cli, you can look at the 2 previous comment and you will find a github repo where you can see the bug. https://github.com/juleskreutzer/lb4-repository-test
@ybarbaria, it seems like you can get rid of this issue if you change this line: https://github.com/juleskreutzer/lb4-repository-test/blob/master/src/datasources/test.datasource.ts#L18
from:
let config: DatasourceConfiguration;
to:
let config: DatasourceConfiguration = {
name: 'mongodb',
connector: 'mongodb',
};
IIUC, when lb4 repository is run, the command only take the file, parse it and look for certain NodeObject. So it doesn't know the value you want to set for config yet.
Hope it helps.
@dhmlau Thanks I will try your solution ! :)
Closing due to inactivity.