我正在本地机器上为我的本地数据库创建一个新的event
,而我的时区偏移量正在被服务器复制...
当我的应用程序的用户创建event
时,客户端脚本会在将偏移时间发送到服务器之前计算和adds输入的TZ偏移量.你可以在我下面的req.body
中看到这一点,在 Select 了9am CST开始和10am CST结束后(当前CST偏移是6小时).
它看起来很完美:
req.body {
event_name: '9am start from local machine to local db',
description: 'Testing the timezone offset',
type: 'mens basketball',
event_start: '2023-12-13T15:00', // 09:00 + 06:00 = 15:00
event_end: '2023-12-13T16:00', // 10:00 + 06:00 = 16:00
location_name: 'Home',
address: '111 West Ave, Austin, TX 78703'
}
这是从服务器端的createEvent
函数记录的:
module.exports.createEvent = async (req, res) => {
console.log("req.body ", req.body); // req.body from above
try {
// if req.body.address is blank, prevent event.geometry from being set
const geoData = await geocoder.forwardGeocode({
query: req.body.address,
limit: 1
}).send()
const event = new Event(req.body);
if (req.body.address === '') {
event.geometry.type = 'Point';
event.geometry.coordinates = [0, 0];
} else {
event.geometry = geoData.body.features[0].geometry;
}
event.user = req.user._id;
console.log("event ", event); // here, the start and end times have added 12 hours instead of 6
await event.save();
req.flash('success', 'Successfully made a new event!');
res.redirect(`/events/${event._id}/addimage`)
} catch (e) {
req.flash('error', e.message);
res.redirect('new');
}
}
正如console.log("event ", event)
旁边的 comments 中提到的,我的开始和结束时间不知何故在偏移量中增加了6个小时,使完整的偏移量达到12个小时.这可以在控制台中记录的event
对象中看到:
event {
event_name: '9am start from local machine to local db',
description: 'Testing the timezone offset',
location_name: 'Home',
address: '111 West Ave, Austin, TX 78703',
geometry: { type: 'Point', coordinates: [ -97.727415, 30.33413 ] },
event_start: 2023-12-13T21:00:00.000Z, // 09:00 + 12:00 = 12:00
event_end: 2023-12-13T22:00:00.000Z, // 10:00 + 12:00 = 22:00
type: 'mens basketball',
activations: [],
fans: [],
_id: new ObjectId("6579c977e470980be127375c"),
image: [],
created: 2023-12-13T15:10:47.231Z,
user: new ObjectId("63bd61b0793748758b8ef3fb")
}
另有stack overflow post条提到MongoDB Compass有时会给你调整好的时间,误导开发人员.然而,我确认我的MongoDB指南针和我的Mongoh终端shell 与控制台中的event
对象具有相同的12小时偏移量.
最疯狂的是我的制作应用程序没有这个问题……我的开始和结束时间正确地存储在我的MongoDB Atlas上,并正确地呈现在客户端上.生产应用程序正在设置为UTC 00:00的Heroku服务器上运行.
我在两个环境中都在运行Mongoose、Express、Node和Ejs.
为什么会这样?