gpt4 book ai didi

mysql - 如何在react js中从mysql中的表中呈现正确的日期

转载 作者:行者123 更新时间:2023-11-29 09:25:52 24 4
gpt4 key购买 nike

我有点卡住了,我在 MySQL 中有一个表,其中有 4 个读数,日期字段中的日期不同,当我渲染它们时,它们都显示今天的日期...为什么?我做错了什么?这是我的代码...

getReading = user_id => { const { 读数 } = this.state;

if (
readings.length > 0 &&
readings.find(reading => reading.user_id === user_id)
) {
this.setState({
reading: {
...this.state.reading,
user_id: readings.find(reading => reading.user_id === user_id)
.user_id,
date: readings.find(reading => reading.user_id === user_id).date,
sugarB: readings.find(reading => reading.user_id === user_id).sugarB,
carbsB: readings.find(reading => reading.user_id === user_id).carbsB,
insulinB: readings.find(reading => reading.user_id === user_id)
.insulinB,
insulinSB: readings.find(reading => reading.user_id === user_id)
.insulinSB,
insulinFB: readings.find(reading => reading.user_id === user_id)
.insulinFB,
sugarL: readings.find(reading => reading.user_id === user_id).sugarL,
carbsL: readings.find(reading => reading.user_id === user_id).carbsL,
insulinL: readings.find(reading => reading.user_id === user_id)
.insulinL,
hbp: readings.find(reading => reading.user_id === user_id).hbp,
lbp: readings.find(reading => reading.user_id === user_id).lbp,
hr: readings.find(reading => reading.user_id === user_id).hr,
sugarD: readings.find(reading => reading.user_id === user_id).sugarD,
carbsD: readings.find(reading => reading.user_id === user_id).carbsD,
insulinD: readings.find(reading => reading.user_id === user_id)
.insulinD
}
});
}

};我的状态看起来像这样...... 读数:[], 阅读: { 用户 ID: 0, 日期:new Date().toLocaleDateString(), 糖B:0, 碳水化合物B: 0, 胰岛素B: 0, 胰岛素SB: 0, 胰岛素FB:0, 糖L:0, 碳水化合物L: 0, 胰岛素L: 0, 热结合蛋白: 0, 磅数:0, 小时:0, 糖D:0, 碳水化合物D: 0, 胰岛素D:0 },

最佳答案

您可以将代码更新为此代码,它将执行完全相同的操作,但更易于阅读。 SQL 中的日期可能为空。在渲染函数中,您可能会执行类似 new Date("from SQL"=> null) 的操作。

const currentReading = readings.find(reading => reading.user_id === user_id);
if (
readings.length &&
currentReading
) {
this.setState({
reading: {
...this.state.reading,
...currentReading
}
});
}

关于mysql - 如何在react js中从mysql中的表中呈现正确的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59618641/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com