刷新网站后使用效果停止工作
我在Reactjs工作。我的表格被认为是另一种形式的配置。这种表格形式的外观是这样的:
const [startingDate, setStartingDate] = useState();
const [endingDate, setEndingDate] = useState();
const [startingTime, setStartingTime] = useState();
const [endingTime, setEndingTime] = useState();
const [places, setPlaces] = useState();
const createConfig = () => {
Axios.post("http://localhost:3005/adminConfig", {
startingDate,
endingDate,
startingTime,
endingTime,
places,
organization: user._id,
}).then((response) => {
alert("successful")
})
}
将数据发送到服务器后,我应该检索并将其设置为另一种形式的配置(第二个表单应该由其他人共享和填写)方法:
const [config, setConfig] = useState([]);
useEffect(() => {
Axios.get(`http://localhost:3005/adminConfig/`).then((response) => {
setConfig(response.data);
});
}, []);
<input
type="date"
placeholder="Date of the meeting
name="date"
id="datePickerId"
min={config[0].startingDate}
max={config[0].endingDate}
onChange={(event) => {
setDate(event.target.value);
}}
required
/>
当您访问此表单时,尚未设置React中的配置时,效果很好。访问它并启动配置后,您可以查看其更新的方式;日期是有限的,从理论上讲,一切都很好,当您刷新网站或其他人试图访问时,问题出现,它停止工作,您必须删除设置(config [0] .startingdate等)
控制台显示:
< Strong>无法阅读未定义的属性(阅读“起点”)
任何帮助都将不胜感激
I'm working in ReactJS. I have a form that is suppossed to be the configuration of another form. This specif form kind of looks like this:
const [startingDate, setStartingDate] = useState();
const [endingDate, setEndingDate] = useState();
const [startingTime, setStartingTime] = useState();
const [endingTime, setEndingTime] = useState();
const [places, setPlaces] = useState();
const createConfig = () => {
Axios.post("http://localhost:3005/adminConfig", {
startingDate,
endingDate,
startingTime,
endingTime,
places,
organization: user._id,
}).then((response) => {
alert("successful")
})
}
After you send that data to the server I'm supposed to retrieve and set it as the configuration of another form (This second form is supposed to be shared and filled by other people) in the next way:
const [config, setConfig] = useState([]);
useEffect(() => {
Axios.get(`http://localhost:3005/adminConfig/`).then((response) => {
setConfig(response.data);
});
}, []);
<input
type="date"
placeholder="Date of the meeting
name="date"
id="datePickerId"
min={config[0].startingDate}
max={config[0].endingDate}
onChange={(event) => {
setDate(event.target.value);
}}
required
/>
When you access this form and havent set the configuration in React it works good. After accessing it and starting the configuration you can see how it updates; the dates are limited and in theory everything works good, the problem comes when you refresh the site or someone else try to access, it stops working and you have to delete the settings (config[0].startingDate, etc)
console shows:
Cannot read properties of undefined (reading 'startingDate')
Any help is appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
加载页面后,执行
使用效果
挂钩。在初始页面加载时,config [0]
尚未定义。使用可选的链接和默认值togther:
The
useEffect
hook is executed after the page is loaded. and at the time of initial page loading,config[0]
is not defined.use optional chaining and default value togther:
如果可能的话,您可以使用后备值:
有些人没有admin配置,为什么您遇到错误的原因:还可以:
If it is possible, you can use fallback value:
The reason why you've got an error as some people do not have admin config and it is okay:
尝试使Axios请求
async
也可以返回空数组。Try making the axios request
async
too so that it doesn't return an empty array.您可以使用可选的链接来实现所需的行为,可选的链式操作员(?)使您可以读取位于连接对象链深处的属性的值,而无需检查链中的每个引用是否有效。
You can use optional chaining to achieve the required behavior, The optional chaining operator (?.) enables you to read the value of a property located deep within a chain of connected objects without having to check that each reference in the chain is valid.