From b90ade750ed204bfe42c72bb97da1f56ae0e006a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E9=A3=9E=E6=B4=8B?= <15778543+xufeiyang6017@user.noreply.gitee.com> Date: Wed, 28 Jan 2026 14:10:53 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=9B=8B=E5=90=A7=E9=A1=B5?= =?UTF-8?q?=E9=9D=A2=E8=8E=B7=E5=8F=96=E6=89=80=E6=9C=89=E5=8A=A8=E6=80=81?= =?UTF-8?q?=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server-example/server-mysql.js | 40 +++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/server-example/server-mysql.js b/server-example/server-mysql.js index efc922c..601d0b0 100644 --- a/server-example/server-mysql.js +++ b/server-example/server-mysql.js @@ -194,17 +194,26 @@ app.get('/api/eggbar/posts', async (req, res) => { offset }); + // 使用新的 Sequelize 实例查询 eggbar 数据库 + const tempSequelize = new Sequelize('eggbar', dbConfig.user, dbConfig.password, { + host: dbConfig.host, + port: dbConfig.port, + dialect: 'mysql', + logging: console.log, + timezone: '+08:00' + }); + // 从数据库获取帖子列表 - const [posts, metadata] = await eggbarSequelize.query( - `SELECT * FROM eggbar_posts - ORDER BY created_at DESC - LIMIT ? OFFSET ?`, + const posts = await tempSequelize.query( + 'SELECT * FROM eggbar_posts ORDER BY created_at DESC', { - replacements: [pageSize, offset], - type: eggbarSequelize.QueryTypes.SELECT + type: tempSequelize.QueryTypes.SELECT } ); + // 关闭临时连接 + await tempSequelize.close(); + console.log('原始查询结果:', posts); console.log('查询结果类型:', typeof posts); console.log('是否为数组:', Array.isArray(posts)); @@ -212,18 +221,19 @@ app.get('/api/eggbar/posts', async (req, res) => { // 确保查询结果为数组 const postsArray = Array.isArray(posts) ? posts : (posts ? [posts] : []); - // 获取总帖子数 - const [[totalCount]] = await eggbarSequelize.query( - 'SELECT COUNT(*) as count FROM eggbar_posts' - ); + // 手动处理分页 + const totalCount = postsArray.length; + const startIndex = (page - 1) * pageSize; + const endIndex = startIndex + pageSize; + const paginatedPosts = postsArray.slice(startIndex, endIndex); console.log('查询结果:', { - postCount: postsArray.length, - totalCount: totalCount.count + postCount: paginatedPosts.length, + totalCount: totalCount }); // 格式化响应数据 - const formattedPosts = postsArray.map(post => ({ + const formattedPosts = paginatedPosts.map(post => ({ id: post.id, user_id: post.user_id, phone: post.phone, @@ -247,8 +257,8 @@ app.get('/api/eggbar/posts', async (req, res) => { pagination: { page, pageSize, - total: totalCount.count, - totalPages: Math.ceil(totalCount.count / pageSize) + total: totalCount, + totalPages: Math.ceil(totalCount / pageSize) } } });