在 ReactJS 中获取 JSON 验证列表的错误
Get the error of a JSON Validation List in ReactJS
在我的 api 测试中,它能够 运行 如下图所示。但是,当我尝试在 React.JS 中编码时,无法传入用户 ID。这是什么问题?
但是当我尝试在前端编写代码时,无法将用户 ID 传递给 api。
考虑以下代码:
constructor (props){
super(props);
const Users_id = localStorage.getItem('id');
this.state ={
users_id: Users_id,
event_name: '',
event_email: '',
event_description: '',
event_type: '',
event_location: '',
start_date: '',
end_date: '',
history: PropTypes.object.isRequired
}
this.create = this.create.bind(this);
this.onChange = this.onChange.bind(this);
}
create(){
// const loginEmail = localStorage.getItem('loginEmail');
const Users_id = localStorage.getItem('id');
this.setState({Users_id})
// console.log(loginEmail)
PostData('api/event/submit', this.state).then ((result) => {
let responseJSON = result;
console.log(responseJSON);
console.log(this.state)
});
}
console.log(responseJSON) 中显示的错误:
//更新的 PostData
return new Promise((resolve, reject) => {
fetch( BaseUrl+type, {
method: "POST",
body: JSON.stringify(userData),
Accept: 'application/json',
// mode: 'no-cors',
headers:
{
'Content-Type': 'application/json'
},
})
.then((response) => response.json())
.then((responseJson) => {
resolve(responseJson);
})
.catch((error)=>{
console.error('Error:', error);
})
});
Storage.getItem returns 一个字符串。 id 应转换为数字
const Users_id = parseInt(localStorage.getItem('id'));
您可能需要在发送前检查 isNaN(Users_id)
。或者让它失败,然后处理错误。
在将其插入 props 之前,您需要检查本地存储中是否存在用户 ID
super(props);
const Users_id = localStorage.getItem('id') || '';
this.state ={
users_id: Users_id,
event_name: '',
event_email: '',
event_description: '',
event_type: '',
event_location: '',
start_date: '',
end_date: '',
history: PropTypes.object.isRequired
}
并且当您想要设置状态时,您需要传递您在状态中使用的相同名称
const Users_id = localStorage.getItem('id');
if(Users_id){
this.setState({users_id: Users_id})
}
在我的 api 测试中,它能够 运行 如下图所示。但是,当我尝试在 React.JS 中编码时,无法传入用户 ID。这是什么问题?
但是当我尝试在前端编写代码时,无法将用户 ID 传递给 api。 考虑以下代码:
constructor (props){
super(props);
const Users_id = localStorage.getItem('id');
this.state ={
users_id: Users_id,
event_name: '',
event_email: '',
event_description: '',
event_type: '',
event_location: '',
start_date: '',
end_date: '',
history: PropTypes.object.isRequired
}
this.create = this.create.bind(this);
this.onChange = this.onChange.bind(this);
}
create(){
// const loginEmail = localStorage.getItem('loginEmail');
const Users_id = localStorage.getItem('id');
this.setState({Users_id})
// console.log(loginEmail)
PostData('api/event/submit', this.state).then ((result) => {
let responseJSON = result;
console.log(responseJSON);
console.log(this.state)
});
}
console.log(responseJSON) 中显示的错误:
//更新的 PostData
return new Promise((resolve, reject) => {
fetch( BaseUrl+type, {
method: "POST",
body: JSON.stringify(userData),
Accept: 'application/json',
// mode: 'no-cors',
headers:
{
'Content-Type': 'application/json'
},
})
.then((response) => response.json())
.then((responseJson) => {
resolve(responseJson);
})
.catch((error)=>{
console.error('Error:', error);
})
});
Storage.getItem returns 一个字符串。 id 应转换为数字
const Users_id = parseInt(localStorage.getItem('id'));
您可能需要在发送前检查 isNaN(Users_id)
。或者让它失败,然后处理错误。
在将其插入 props 之前,您需要检查本地存储中是否存在用户 ID
super(props);
const Users_id = localStorage.getItem('id') || '';
this.state ={
users_id: Users_id,
event_name: '',
event_email: '',
event_description: '',
event_type: '',
event_location: '',
start_date: '',
end_date: '',
history: PropTypes.object.isRequired
}
并且当您想要设置状态时,您需要传递您在状态中使用的相同名称
const Users_id = localStorage.getItem('id');
if(Users_id){
this.setState({users_id: Users_id})
}