我无法从我的数据库中读取带有 get in react 的 json 对象



我的问题是,当我使用带有"得到";方法,然后我尝试将响应从sql中的数据库传递到具有useState的对象,它会向我抛出一个空对象,而当我通过控制台将其打印到数据库中的Answer时,会向我投掷一个包含所有数据的json。附件截图

//Mi hook
const [items, setItems] = useState({});
useEffect(() => {
//isSuscribed prevents react from failing when the database is //restarting
let isSuscribed = true
if (isSuscribed) {
if (submmit && Object.keys(error).length===0 ) {
let {email,password} = values;
//URL  of my local api            
var Url = `http://localhost:4000/BD/Usuarios/validate/${email}/${password}`;
//setValues({email:"",password:""});
fetch(Url)
.then(res=>{
setLoading(true);
/*
Some code here 
*/
res.json()
}).then(data=>{
console.log(data);
//{name: "name", lastname: "lastname", contraseña: "handleemail", email: "email@hotmail.com"}
setLoading(false);
setProblem(null);
setItems(data);
console.log(data.name);
//undefined
console.log(items);
//{name: null, lastname: "", contraseña: "", email: ""} 
})
.catch((error)=>{
console.warn(`New Error ${error}`);
});


console.log(FakeAuth.tokens);

if (FakeAuth.tokens.name) {
auth.signin(()=>{
history.replace(from);
});
}
};
}

return () => isSuscribed=false
}, [error]);

现在,当我再次刷新时,我的页面只会更新我的useState,项目就会获得值​​我的api 发送的数据

我将冒险进行猜测,并说它返回了一个字符串,然后需要使用data=JSON.parse(data)进行解析。Fetch API将返回一个字符串。

最新更新