在循环中调用 conn.query 导致:变量 'conn' 隐式具有 'any' 类型
Calling conn.query in a loop causes: Variable 'conn' implicitly has an 'any' type
我有一条如下所示的路线,我想在其中从数据库中获取所有 posts(如在 Twitter 中)。我已将每个 post 的喜欢和图像分开在其自己的 sql table 中,称为“post_likes”和“post_images”。
检索 post 后,我想为每个 post 添加喜欢,然后将其发送回客户端。
但是遍历数组并每次调用 conn.query
都会给我这两个错误:
error TS7034: Variable 'conn' implicitly has type 'any' in some locations where its type cannot be determined.
error TS7005: Variable 'conn' implicitly has an 'any' type.
为什么 typescript 无法确定 map 或 forEach 循环中 conn 的类型?
router.get("/get/:token", async (req: Request, res: Response) => {
/**
* Gets all Post from you and your friends
*/
...
let conn; // ERROR <-- Variable 'conn' implicitly has type 'any' in some locations where its type cannot be determined.
try {
conn = await pool.getConnection();
...
/* get all posts */
const queryGetPostsResult = await conn.query(queryGetPosts);
const getPosts: Array<Post> = [...queryGetPostsResult];
/* add images and likes to each post */
let clientPosts: Array<ClientPost> = await Promise.all(getPosts.map(async (post: Post) => {
/* FIXME get the likes */
const queryGetLikes: string = `SELECT user_id FROM post_likes WHERE post_id=?`
const queryGetLikesResult = await conn.query(queryGetLikes, [post.id]); // ERROR <-- Variable 'conn' implicitly has an 'any' type.
const likes: Array<number> = queryGetLikesResult.map((x: { user_id: number }) => x.user_id);
/* TODO get the images */
const images: Array<string> = [];
const clientPost: ClientPost = {
id: post.id,
writtenBy: post.written_by,
content: post.content,
writtenAt: post.written_at,
images,
likes,
};
return clientPost;
}));
return res.send(clientPosts);
} catch(err: unknown) {
throw console.log(colors.red(`/api/post/get/:token => ${err}`));
} finally {
if (conn) return conn.release();
}
});
喜欢T.J。 Crowder 建议问题是 let conn;
处缺少类型注释,但我仍然不明白为什么打字稿只在我尝试在循环内调用 conn.query()
时抛出错误。如果有人知道请给我解释一下。
我还必须添加 if (!conn) throw "conn is undefined"
以跳出 try & map 块。
所以我将路线编辑为以下内容:
router.get("/get/:token", async (req: Request, res: Response) => {
/**
* Gets all Post from you and your friends
*/
...
let conn: PoolConnection | undefined; // <-- ADDED TO FIX THE ERROR!!!
try {
conn = await pool.getConnection();
if (!conn) throw "conn is unknown"; // <-- ADDED TO FIX THE ERROR!!!
...
/* get all posts */
const queryGetPostsResult = await conn.query(queryGetPosts);
const getPosts: Array<Post> = [...queryGetPostsResult];
/* add images and likes to each post */
let clientPosts: Array<ClientPost> = await Promise.all(getPosts.map(async (post: Post) => {
if (!conn) throw "conn is unknown"; // <-- ADDED TO FIX THE ERROR!!!
/* FIXME get the likes */
const queryGetLikes: string = `SELECT user_id FROM post_likes WHERE post_id=?`
const queryGetLikesResult = await conn.query(queryGetLikes, [post.id]);
const likes: Array<number> = queryGetLikesResult.map((x: { user_id: number }) => x.user_id);
/* TODO get the images */
const images: Array<string> = [];
const clientPost: ClientPost = {
id: post.id,
writtenBy: post.written_by,
content: post.content,
writtenAt: post.written_at,
images,
likes,
};
return clientPost;
}));
return res.send(clientPosts);
} catch(err: unknown) {
throw console.log(colors.red(`/api/post/get/:token => ${err}`));
} finally {
if (conn) return conn.release();
}
});
我有一条如下所示的路线,我想在其中从数据库中获取所有 posts(如在 Twitter 中)。我已将每个 post 的喜欢和图像分开在其自己的 sql table 中,称为“post_likes”和“post_images”。
检索 post 后,我想为每个 post 添加喜欢,然后将其发送回客户端。
但是遍历数组并每次调用 conn.query
都会给我这两个错误:
error TS7034: Variable 'conn' implicitly has type 'any' in some locations where its type cannot be determined.
error TS7005: Variable 'conn' implicitly has an 'any' type.
为什么 typescript 无法确定 map 或 forEach 循环中 conn 的类型?
router.get("/get/:token", async (req: Request, res: Response) => {
/**
* Gets all Post from you and your friends
*/
...
let conn; // ERROR <-- Variable 'conn' implicitly has type 'any' in some locations where its type cannot be determined.
try {
conn = await pool.getConnection();
...
/* get all posts */
const queryGetPostsResult = await conn.query(queryGetPosts);
const getPosts: Array<Post> = [...queryGetPostsResult];
/* add images and likes to each post */
let clientPosts: Array<ClientPost> = await Promise.all(getPosts.map(async (post: Post) => {
/* FIXME get the likes */
const queryGetLikes: string = `SELECT user_id FROM post_likes WHERE post_id=?`
const queryGetLikesResult = await conn.query(queryGetLikes, [post.id]); // ERROR <-- Variable 'conn' implicitly has an 'any' type.
const likes: Array<number> = queryGetLikesResult.map((x: { user_id: number }) => x.user_id);
/* TODO get the images */
const images: Array<string> = [];
const clientPost: ClientPost = {
id: post.id,
writtenBy: post.written_by,
content: post.content,
writtenAt: post.written_at,
images,
likes,
};
return clientPost;
}));
return res.send(clientPosts);
} catch(err: unknown) {
throw console.log(colors.red(`/api/post/get/:token => ${err}`));
} finally {
if (conn) return conn.release();
}
});
喜欢T.J。 Crowder 建议问题是 let conn;
处缺少类型注释,但我仍然不明白为什么打字稿只在我尝试在循环内调用 conn.query()
时抛出错误。如果有人知道请给我解释一下。
我还必须添加 if (!conn) throw "conn is undefined"
以跳出 try & map 块。
所以我将路线编辑为以下内容:
router.get("/get/:token", async (req: Request, res: Response) => {
/**
* Gets all Post from you and your friends
*/
...
let conn: PoolConnection | undefined; // <-- ADDED TO FIX THE ERROR!!!
try {
conn = await pool.getConnection();
if (!conn) throw "conn is unknown"; // <-- ADDED TO FIX THE ERROR!!!
...
/* get all posts */
const queryGetPostsResult = await conn.query(queryGetPosts);
const getPosts: Array<Post> = [...queryGetPostsResult];
/* add images and likes to each post */
let clientPosts: Array<ClientPost> = await Promise.all(getPosts.map(async (post: Post) => {
if (!conn) throw "conn is unknown"; // <-- ADDED TO FIX THE ERROR!!!
/* FIXME get the likes */
const queryGetLikes: string = `SELECT user_id FROM post_likes WHERE post_id=?`
const queryGetLikesResult = await conn.query(queryGetLikes, [post.id]);
const likes: Array<number> = queryGetLikesResult.map((x: { user_id: number }) => x.user_id);
/* TODO get the images */
const images: Array<string> = [];
const clientPost: ClientPost = {
id: post.id,
writtenBy: post.written_by,
content: post.content,
writtenAt: post.written_at,
images,
likes,
};
return clientPost;
}));
return res.send(clientPosts);
} catch(err: unknown) {
throw console.log(colors.red(`/api/post/get/:token => ${err}`));
} finally {
if (conn) return conn.release();
}
});