mysql - 为什么它将与数据库的连接检测为未定义?
问题描述
我正在制作一个基本的 API,但不明白为什么它会给我一个错误。
TypeError: Cannot read property 'conneDB' of undefined
import { json, Request, Response } from "express";
import { ConnectionDB } from "../ConnectionDB";
import Post from "../interfaces/Ipost.interface";
export class PostsController {
private conneDB = new ConnectionDB()
async getAllPosts(req: Request, res: Response) {
const posts = await this.conneDB.connection.query('SELECT * FROM posts')
return res.json(posts[0])
}
async getPost(req: Request, res: Response){
const idPost = req.params.postId;
const post = await this.conneDB.connection.query('SELECT * FROM posts WHERE id = ?', [idPost])
return res.json(post[0])
}
async createPost(req: Request, res: Response) : Promise<Response>{
const newPost: Post = req.body;
await this.conneDB.connection.query('INSERT INTO posts SET ?', [newPost])
return res.json({
message: 'Post created'
});
}
}
解决方案
推荐阅读
- php - PHP Google People API save() 不再工作
- java - 找不到符合条件的 bean 依赖项 [CountryRepository]:预计至少有 1 个符合自动装配候选资格的 bean
- html - 索引范围的第一个字母不可选择
- bash - 如何运行脚本以使用 url 在服务器上构建 Satis
- django - 运行 DRF 序列化程序的结果中的空数据
- javascript - Electron + React Hot Reload + Typescript + SCSS 导致热重载失败
- gcc - Nasm 未被识别为内部或外部命令
- ssl - 我想用JMeter来模拟TLS握手过程
- javascript - VueJS V-bind:sync="{object}" -- 在父节点中观察子节点发出的数据
- c# - 为什么 GetSystemInfo().minimumApplicationAddress 返回的数字大于 GetSystemInfo().maximumApplicationAddress?