// 清除模块缓存 function clearModuleCache() { const modulesToClear = Object.keys(require.cache).filter(key => key.includes('HarmlessPlace') || key.includes('database') ); console.log('清除以下模块的缓存:', modulesToClear.length, '个模块'); modulesToClear.forEach(key => { console.log('-', key); delete require.cache[key]; }); } // 清除缓存后再导入 clearModuleCache(); const axios = require('axios'); const HarmlessPlace = require('./models/HarmlessPlace'); console.log('=== 检查HarmlessPlace模型 ==='); console.log('HarmlessPlace的类型:', typeof HarmlessPlace); console.log('HarmlessPlace是否为对象:', typeof HarmlessPlace === 'object' && HarmlessPlace !== null); console.log('HarmlessPlace是否有findAndCountAll方法:', typeof HarmlessPlace.findAndCountAll !== 'undefined'); if (HarmlessPlace.findAndCountAll) { console.log('findAndCountAll的类型:', typeof HarmlessPlace.findAndCountAll); } // 登录函数 async function login() { try { const response = await axios.post('http://localhost:3000/api/auth/login', { username: 'admin', password: '123456' }); console.log('登录成功,token:', response.data.data.token); return response.data.data.token; } catch (error) { console.error('登录失败:', error.response ? error.response.data : error.message); return null; } } // 测试无害化场所列表API async function testHarmlessPlaceList(token) { try { const response = await axios.get('http://localhost:3000/api/harmless-place/list', { headers: { 'Authorization': `Bearer ${token}` }, params: { page: 1, pageSize: 10 } }); console.log('无害化场所列表API调用成功:', response.data); return response.data; } catch (error) { console.error('无害化场所列表API调用失败:', error.message); if (error.response) { console.error('错误数据:', error.response.data); } return null; } } // 主函数 async function main() { console.log('开始测试无害化场所管理API...'); // 登录获取token const token = await login(); if (!token) { console.log('登录失败,无法继续测试'); return; } // 再次检查模型类型,确保在API调用前没有被修改 console.log('\n=== API调用前再次检查HarmlessPlace模型 ==='); console.log('HarmlessPlace的类型:', typeof HarmlessPlace); console.log('HarmlessPlace是否有findAndCountAll方法:', typeof HarmlessPlace.findAndCountAll !== 'undefined'); // 测试API await testHarmlessPlaceList(token); console.log('\n所有测试完成!'); } // 运行测试 main().catch(err => console.error('测试过程中出错:', err));