2024-08-04
温故知新
00
请注意,本文编写于 43 天前,最后修改于 43 天前,其中某些信息可能已经过时。

目录

简介
代码如下
报错
问题排查
临时解决

简介

使用 Nuxt3 开发网站时,想要使用是sqlite作为数据存储方案,Nuxt3的服务端开发时基于 Nitro 的,而 Nitro 又默认支持 sqlite,这不是巧了吗,一拍即合开搞。

结果在写数据更新(UPDATE)的SQL时,出现了意料之外的问题,特此记录。

代码如下

以下代码就是为了动态拼装SQL,所以写了很多判断,并最终拼接到执行的申请了中,一切看似正常,但执行时却出现了报错,报错信息请向下看。

ts
export default defineEventHandler(async (event) => { const body = await readBody(event); const db = useDatabase(); if (!body.oldname) { return "not find oldname"; } let updatesql = ""; let fileNumber = 0; if (body.name) { fileNumber += 1; updatesql += `name='${body.name}'`; } if (body.author) { if (fileNumber >= 1) { updatesql += `,`; } fileNumber += 1; updatesql += `author='${body.author}'`; } if (body.avatar) { if (fileNumber >= 1) { updatesql += `,`; } fileNumber += 1; updatesql += `avatar='${body.avatar}'`; } // ···省略部分重复带啊吗 const sql = `UPDATE softs ${updatesql} WHERE name = '${body.oldname}'`; await db.sql`UPDATE softs SET ${updatesql} WHERE name='${body.oldname}'`; return "ok"; });

报错

ERROR [nuxt] [request error] [unhandled] [500] near "?": syntax error at Database.prepare (E:\Code\github-soft\node_modules\.pnpm\[email protected]\node_modules\better-sqlite3\lib\methods\wrappers.js:6:21) at Object.prepare (/E:/Code/github-soft/node_modules/.pnpm/[email protected][email protected]/node_modules/db0/connectors/better-sqlite3.mjs:24:29) at Object.sql (/E:/Code/github-soft/node_modules/.pnpm/[email protected][email protected]/node_modules/db0/dist/index.mjs:39:37) at Object.handler (E:\Code\github-soft\server\api\updatesoft.ts:64:1) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async /E:/Code/github-soft/node_modules/.pnpm/[email protected]/node_modules/h3/dist/index.mjs:1975:19 at async Object.callAsync (/E:/Code/github-soft/node_modules/.pnpm/[email protected]/node_modules/unctx/dist/index.mjs:72:16) at async Server.toNodeHandle (/E:/Code/github-soft/node_modules/.pnpm/[email protected]/node_modules/h3/dist/index.mjs:2266:7)

可以看到报错信息并没有什么有价值的玩意,但老狗大概能猜出了这是 SQL 的问题,关键是问题在哪呢?不明觉厉。

问题排查

想要排查该问题,我的思路就是找到 SQL 中的问题并解决,随便点进报错的内容中E:\Code\github-soft\node_modules\.pnpm\[email protected]\node_modules\better-sqlite3\lib\methods\wrappers.js:6:21,发现这里参数只有一个sql,代码如下:

ts
exports.prepare = function prepare(sql) { return this[cppdb].prepare(sql, this, false); };

所以在这里加个日志,即:

ts
exports.prepare = function prepare(sql) { console.log(sql) return this[cppdb].prepare(sql, this, false); };

随后发现,这里的日志显示的sql为:UPDATE softs SET ? WHERE name='?',老狗直接黑人号???

问题果然发生在SQL上,看起来 better-sqlite3db.sql后面的计算属性作为了SQL参数位来预置SQL,但我的计算属性并不只有参数,因此导致了问题。

临时解决

发现了问题后,就是解决了,办法就是计算属性只用在参数为上,那么怎么改呢?挠头。。。

PS:解决的过程无力吐槽,最终暂时用下面的代码能跑了,不要说这段代码有多烂,我知道!

ts
export default defineEventHandler(async (event) => { const body = await readBody(event); const db = useDatabase(); if (!body.oldname) { return "not find oldname"; } if (body.name) { await db.sql`UPDATE softs SET name=${body.name} WHERE name=${body.oldname}`; } if (body.author) { await db.sql`UPDATE softs SET author=${body.author} WHERE name=${body.oldname}`; } if (body.avatar) { await db.sql`UPDATE softs SET avatar=${body.avatar} WHERE name=${body.oldname}`; } if (body.desc) { await db.sql`UPDATE softs SET desc=${body.desc} WHERE name=${body.oldname}`; } if (body.github) { await db.sql`UPDATE softs SET github=${body.github} WHERE name=${body.oldname}`; } if (body.tags) { await db.sql`UPDATE softs SET tags=${body.tags} WHERE name=${body.oldname}`; } if (body.files) { await db.sql`UPDATE softs SET files=${body.files} WHERE name=${body.oldname}`; } if (body.show) { await db.sql`UPDATE softs SET show=${body.show} WHERE name=${body.oldname}`; } return "ok"; });
如果对你有用的话,可以打赏哦
打赏
ali pay
wechat pay

本文作者:DingDangDog

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!