/** * 检查当前数据库中的经纬度数据 * @file check-current-data.js */ const { Farm } = require('./models'); const { sequelize } = require('./config/database-simple'); async function checkCurrentData() { try { console.log('连接数据库...'); await sequelize.authenticate(); console.log('\n查询最近的养殖场记录...'); const farms = await Farm.findAll({ attributes: ['id', 'name', 'location', 'created_at'], order: [['id', 'DESC']], limit: 10 }); console.log(`\n找到 ${farms.length} 条记录:`); console.log('=' .repeat(80)); farms.forEach((farm, index) => { console.log(`${index + 1}. ID: ${farm.id}`); console.log(` 名称: ${farm.name}`); console.log(` Location对象: ${JSON.stringify(farm.location)}`); if (farm.location && typeof farm.location === 'object') { const lng = farm.location.lng; const lat = farm.location.lat; console.log(` 经度 (lng): ${lng} (类型: ${typeof lng})`); console.log(` 纬度 (lat): ${lat} (类型: ${typeof lat})`); if (lng !== undefined || lat !== undefined) { console.log(` ✅ 有经纬度数据`); } else { console.log(` ❌ 无经纬度数据`); } } else { console.log(` ❌ Location字段为空或格式错误`); } console.log(` 创建时间: ${farm.created_at}`); console.log('-'.repeat(60)); }); // 查找包含经纬度数据的记录 console.log('\n查找包含经纬度数据的记录...'); const farmsWithLocation = await Farm.findAll({ where: sequelize.literal("JSON_EXTRACT(location, '$.lng') IS NOT NULL OR JSON_EXTRACT(location, '$.lat') IS NOT NULL"), attributes: ['id', 'name', 'location'], order: [['id', 'DESC']], limit: 5 }); console.log(`\n包含经纬度数据的记录 (${farmsWithLocation.length} 条):`); farmsWithLocation.forEach(farm => { console.log(`ID: ${farm.id}, 名称: ${farm.name}`); console.log(`经度: ${farm.location.lng}, 纬度: ${farm.location.lat}`); console.log(''); }); } catch (error) { console.error('查询失败:', error.message); if (error.sql) { console.error('SQL:', error.sql); } } finally { await sequelize.close(); console.log('数据库连接已关闭'); } } // 运行检查 if (require.main === module) { checkCurrentData(); } module.exports = { checkCurrentData };