R
Routfin
Guest
When trying to make a request using postman, my console returns the error:
(node:25151) UnhandledPromiseRejectionWarning: ConnectionNotFoundError: Connection "default" was not found.
Apparently I can create the tables in the database, but I can't send the requests.
controller:
config database
Answer for: "TypeORM Error sending information to the database"...
(node:25151) UnhandledPromiseRejectionWarning: ConnectionNotFoundError: Connection "default" was not found.
Apparently I can create the tables in the database, but I can't send the requests.
Mã:
import app from './app';
import { AppDataSource } from './src/database/Config';
async function connection() {
const port = process.env.PORT;
try {
await AppDataSource.initialize();
console.log('Database connected! ');
app.listen(port);
console.log(`Listening on port ${port}`)
} catch (error) {
console.log(error);
}
}
connection();
controller:
Mã:
import { Request, Response } from "express";
import { getRepository } from "typeorm";
import { User } from "../entities/User";
class UserController {
async store(req: Request, res: Response) {
const repository = getRepository(User);
const { email, username, password } = req.body;
const userExists = await repository.findOne({ where: { email } });
if (userExists) {
return res.status(409).json('User already exists');
}
const user = repository.create({ email, username, password });
await repository.save(user);
return res.status(200).json(user);
}
}
export default new UserController();
config database
Mã:
import { DataSource } from "typeorm"
import { User } from "../entities/User"
export const AppDataSource = new DataSource({
type: "postgres",
host: process.env.DATABASE_HOST,
port: 5432,
username: process.env.DATABASE_USERNAME,
password: process.env.DATABASE_PASSWORD,
database: process.env.DATABASE_NAME,
synchronize: true,
logging: true,
entities: [User],
subscribers: [],
migrations: [],
})
Answer for: "TypeORM Error sending information to the database"...