I have a backend with NestJS, it is configured with mongoose. The app.module.ts :
#Module({
imports: [
MongooseModule.forRoot('mongodb://admin:root#localhost:27017/'),
CatsModule
],
controllers: [],
providers: [],
})
If I let the URI like this it works correctly but creates a 'tests' database. I want a custom database, so when I try to set the URI to mongodb://admin:root#localhost:27017/my-custom-db it cant connect...
I've tried to create my-custom-db manually, but it still fails
The error :
[Nest] 14480 - 26/06/2022, 12:28:27 ERROR [MongooseModule] Unable to connect to the database. Retrying (1)...
The Problem was because Mongoose use a diferent format to connect to a specific database, we have to pass the name by option:
#Module({
imports: [
MongooseModule.forRoot(
'mongodb://localhost:27017',
{useNewUrlParser:true,user:"root",pass:"example",dbName:"nest"}),
UserModule],
controllers: [AppController],
providers: [AppService],
})
Related
I am making a sign up page with 3 providers (Twitter, Facebook and Instagram) using next-auth and prisma with mongoDB. The issue appears when I try to sign up with any of the providers. Here is my nextauth.js file.
import NextAuth from "next-auth"
import { PrismaAdapter } from "#next-auth/prisma-adapter"
import { PrismaClient } from '#prisma/client';
import InstagramProvider from "next-auth/providers/instagram";
import TwitterProvider from "next-auth/providers/twitter";
import FacebookProvider from "next-auth/providers/facebook";
const prisma = new PrismaClient();
export default NextAuth({
adapter: PrismaAdapter(prisma),
providers: [
InstagramProvider({
clientId: process.env.INSTAGRAM_CLIENT_ID,
clientSecret: process.env.INSTAGRAM_CLIENT_SECRET
}),
TwitterProvider({
clientId: process.env.TWITTER_CLIENT_ID,
clientSecret: process.env.TWITTER_CLIENT_SECRET,
version: "2.0",
}),
FacebookProvider({
clientId: process.env.FACEBOOK_CLIENT_ID,
clientSecret: process.env.FACEBOOK_CLIENT_SECRET
}),
],
session: {
strategy: 'jwt',
},
});
I have tried to reinstall all the dependencies, because I don't see what else could be the problem. At first I thought it was the dependencies so I reinstall all the dependencies.
The problem is in your adapter in the [nextauth].js file or wherever you are declaring the prisma instance.
Check out those similar discussions:
https://github.com/nextauthjs/next-auth/discussions/4152
Integrating Prisma Adapter with Next Auth - Unexpected token 'export'
The issue actually comes from the prisma schema. I fixed it after reading the next auth documentation about prisma and mongoDB.
I'm using windows 10, Nest.js and typeORM.
Previously, I had to create a new user\role uder my windows user - but now the ORM don't seem to find my db (called steamClone) - and calls a db named after my user:
error: database "Itay" does not exist
typeORM is configured like such encapsulated in a module:
import { Module } from '#nestjs/common';
import { TypeOrmModule } from '#nestjs/typeorm';
import {ConfigModule, ConfigService} from '#nestjs/config';
#Module({
imports: [
TypeOrmModule.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: (ConfigService: ConfigService) => ({
type: 'postgres',
host: ConfigService.get('DB_HOST'),
port: ConfigService.get('DB_PORT'),
username: ConfigService.get('DB_USER'),
password: ConfigService.get('DB_PASSWORD'),
database: ConfigService.get('DB_DATABASE'),
entities: [__dirname + '/../**/*.entity{.ts,.js}'],
synchronize: true,
})
})
],
})
export class DatabaseModule {}
all of the parameters are stored in a .env file
running on localhost, default port and everything, postgres user etc.
would love to know how to solve this
I am building an api using nestjs. After adding the typeorm and pg dependencies and adding the TypeOrmModule.forRoot({}) code in app.module.ts like shown below.
import { Module } from '#nestjs/common';
import { TypeOrmModule } from '#nestjs/typeorm';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { CoffeesModule } from './coffees/coffees.module';
#Module({
imports: [CoffeesModule, TypeOrmModule.forRoot({
type: 'postgres',
host: 'localhost',
port: 5432,
username: 'postgres',
password: 'xxx',
database: 'postgres',
autoLoadEntities: true,
synchronize: true
})],
controllers: [AppController],
providers: [AppService],
})
export class AppModule { }
I get an error TypeError: rxjs_1.lastValueFrom is not a function with but no error when I exclude TypeOrmModule.forRoot({}).
What could be the reason for the error ?
If you're using Nest v8, RxJS version 7 is used, which no longer has a toPromise() method for Observables, so Nest uses the lastValueFrom method instead. If you're receiving this error, you probably need to update your rxjs dependency to >7.
npm i rxjs#^7
yarn add rxjs#^7
pnpm i rxjs #^7
Pick your flavor of package manager and have at it.
In the last update of NestJS, when is used cli to initialization of project this error is throw.
The real answer
The issue is conflict with nest version.. anyone who see this - just make sure all your nestJs packages are of version 7 or 8 - don't mix them. especially those:
#nestjs/common
#nestjs/core
#nestjs/typeorm
from here: https://github.com/nestjs/nest/issues/7468#issuecomment-876174870
update "#nestjs/typeorm": "^7.1.5" in package.json and enter npm i and restart server
I am working on nestjs and mongoose recently. I put my MongoDB connection links on .env file. I have configured my nestjs application using ConfigModule. I followed the nestjs docs. But I have no idea why env variables are not working for me.
I have one extra module.ts file called content.module.ts in which I am connecting to mongodb. I have configured this ConfigModule in my app.module.ts and put it as global.
// app.module.ts
#Module({
imports: [
ConfigModule.forRoot({
envFilePath: './config/.env',
isGlobal: true,
}),
ContentModule, //another module
],
})
export class AppModule {}
// content.module.ts
#Module({
imports: [
MongooseModule.forRoot(process.env.CONTENT_DB_CONNECTION_STRING),
MongooseModule.forFeature([{ name: 'App', schema: AppSchema },]),
],
})
export class ContentModule {}
When I run my project using the debugger, I found that MongooseModule was first getting executed in the content.module.ts file, and then configModule was getting executed.
I even tried to configure the configModule in the content.module.ts file itself like below but no luck.
#Module({
imports: [
ConfigModule.forRoot({
envFilePath: '../../config/.env',
}),
MongooseModule.forRoot(process.env.CONTENT_DB_CONNECTION_STRING),
MongooseModule.forFeature([{ name: 'App', schema: AppSchema },]),
],
})
export class ContentModule {}
.env file
PORT=3000
CONTENT_DB_CONNECTION_STRING=mongodb://localhost:27017/content
project structure
I didn't get why mongoose dependencies are getting executed before the nestjs dependencies? And I also need help to fix this env variable.
If I config mongo with nest js its come like this,
my files like this,
app.module.ts
item.schema.ts
items.module.ts
items.service.ts
I use a forwardRef in the other modules where I declare the mongo models like this:
#Module({
imports: [
forwardRef(() => AppModule),
MongooseModule.forFeature([{ name: 'User', schema: UserSchema }])
]
})
Where AppModule my app.module is
Just remove the ItemsService from the AppModule and exports it on your ItemsModule, besides no need to define the ItemsController on the AppModule it's enough to be defined on the ItemsModule.
You may have a look about contollers and exports following this link https://docs.nestjs.com/modules