新增删除功能代码
// model/weaponSkinModel.js(新增)
// 👉 第3个功能:按 ID 删除数据
async function deleteWeaponSkin(id) {
if (!id) throw new Error('ID 不能为空');
try {
const [result] = await pool.execute(
'DELETE FROM weapon_skins WHERE id = ?',
[id]
);
if (result.affectedRows === 0) throw new Error(未找到 ID=${id} 的数据);
return { success: true, message: ID=${id} 数据已删除 };
} catch (error) {
console.error(❌ 删除 ID=${id} 失败:, error.message);
throw error;
}
}
// 最终暴露所有功能
module.exports = {
getAllWeaponSkins,
addWeaponSkin,
deleteWeaponSkin // 新增
};
在index.js中增加测试代码
// index.js(新增测试函数)
// 👉 测试 3:删除数据(对应 deleteWeaponSkin 功能)
async function testDeleteWeaponSkin() {
console.log('===== 开始测试:删除数据 =====');
try {
await testPoolConnection();
// 执行删除:这里的id需要跟数据库保持一致
const result = await weaponSkinModel.deleteWeaponSkin(2);
console.log('删除结果:', result);
} catch (error) {
console.log('删除失败!原因:', error.message);
}
console.log('===== 测试结束:删除数据 =====\n');
}
// 👉 执行当前测试
testDeleteWeaponSkin();