Browse Source

进一步修复位置信息写入数据库的问题,确保JSON字符串正确处理

pull/1/head
SwTt29 2 months ago
parent
commit
7efdcfaddf
  1. 10
      pages/profile/index.js
  2. 38
      server-example/server-mysql.js

10
pages/profile/index.js

@ -779,7 +779,7 @@ Page({
// 构造上传的数据,包含authorized_region字段和必要的phoneNumber
let userInfo = {
authorized_region: locationData // 直接传递对象,由API层处理JSON序列化
authorized_region: JSON.stringify(locationData) // 直接在前端转换为JSON字符串,确保数据格式正确
};
// 确保包含phoneNumber字段(服务器接口要求)
@ -817,7 +817,13 @@ Page({
}
console.log('位置上传前检查openid:', openid);
console.log('准备上传的位置数据:', userInfo);
console.log('位置数据对象:', locationData);
console.log('位置数据类型:', typeof locationData);
console.log('是否有经纬度:', locationData.latitude && locationData.longitude);
console.log('准备上传的完整数据:', {
...userInfo,
openid: openid
});
// 调用API上传位置数据
const api = require('../../utils/api.js');

38
server-example/server-mysql.js

@ -1212,20 +1212,41 @@ app.post('/api/user/upload', async (req, res) => {
where: { openid: userData.openid }
});
// 详细日志记录收到的位置数据
console.log('处理位置数据前:', {
authorized_region: userData.authorized_region,
type: typeof userData.authorized_region,
isObject: typeof userData.authorized_region === 'object',
hasLatLng: userData.authorized_region && (userData.authorized_region.latitude || userData.authorized_region.longitude)
});
if (user) {
// 更新用户信息,确保authorized_region字段正确处理
const updateData = { ...userData, updated_at: getBeijingTime() };
// 特别处理authorized_region字段,如果是对象则转换为JSON字符串
// 特别处理authorized_region字段,只有当它是对象时才转换为JSON字符串
if (updateData.authorized_region && typeof updateData.authorized_region === 'object') {
updateData.authorized_region = JSON.stringify(updateData.authorized_region);
} else if (updateData.authorized_region === null || updateData.authorized_region === undefined) {
// 如果是null或undefined,设置为空字符串
updateData.authorized_region = '';
} else if (typeof updateData.authorized_region === 'string' && updateData.authorized_region.trim() === '') {
// 如果是空字符串,保持为空字符串
updateData.authorized_region = '';
} else if (typeof updateData.authorized_region === 'string') {
// 如果已经是字符串,保持不变
console.log('authorized_region已经是字符串,无需转换:', updateData.authorized_region);
}
console.log('更新用户数据:', { authorized_region: updateData.authorized_region });
await User.update(updateData, {
where: { openid: userData.openid }
});
user = await User.findOne({ where: { openid: userData.openid } });
console.log('更新后用户数据:', { authorized_region: user.authorized_region });
// 使用统一的关联记录创建函数
await createUserAssociations(user);
} else {
@ -1237,13 +1258,26 @@ app.post('/api/user/upload', async (req, res) => {
updated_at: getBeijingTime()
};
// 特别处理authorized_region字段,如果是对象则转换为JSON字符串
// 特别处理authorized_region字段,只有当它是对象时才转换为JSON字符串
if (createData.authorized_region && typeof createData.authorized_region === 'object') {
createData.authorized_region = JSON.stringify(createData.authorized_region);
} else if (createData.authorized_region === null || createData.authorized_region === undefined) {
// 如果是null或undefined,设置为空字符串
createData.authorized_region = '';
} else if (typeof createData.authorized_region === 'string' && createData.authorized_region.trim() === '') {
// 如果是空字符串,保持为空字符串
createData.authorized_region = '';
} else if (typeof createData.authorized_region === 'string') {
// 如果已经是字符串,保持不变
console.log('authorized_region已经是字符串,无需转换:', createData.authorized_region);
}
console.log('创建用户数据:', { authorized_region: createData.authorized_region });
user = await User.create(createData);
console.log('创建后用户数据:', { authorized_region: user.authorized_region });
// 使用统一的关联记录创建函数
await createUserAssociations(user);
}

Loading…
Cancel
Save