Javascript 反应:引用错误:未定义obj
我试图在屏幕上呈现一个特定的组件,条件是我得到一个解析的承诺,数据为Javascript 反应:引用错误:未定义obj,javascript,reactjs,promise,react-hooks,Javascript,Reactjs,Promise,React Hooks,我试图在屏幕上呈现一个特定的组件,条件是我得到一个解析的承诺,数据为{result:“clear”},尽管将if条件放在setTimeout()中我仍然得到obj没有定义即使承诺是先解决的 const useOnfidoFetch = (URL) => { const [token, setToken] = useState(); const [id, setId] = useState(); const [isClear, setIsClear] = useState(fal
{result:“clear”}
,尽管将if
条件放在setTimeout()中
我仍然得到obj没有定义
即使承诺是先解决的
const useOnfidoFetch = (URL) => {
const [token, setToken] = useState();
const [id, setId] = useState();
const [isClear, setIsClear] = useState(false);
useEffect(() => {
axios
.get("http://localhost:5000/post_stuff")
.then((response) => response.data.data.data.json_data)
.then((json_data) => {
console.log("this is the json data", json_data);
const id = json_data.applicant_id;
const token = json_data.onfido_sdk_token;
setId(id);
setToken(token);
});
}, [URL]);
useEffect(() => {
if (!token) return;
console.log("this is working!");
OnfidoSDK.init({
token,
containerId: "root",
steps: [
{
type: "welcome",
options: {
title: "Open your new bank account",
},
},
"document",
],
onComplete: function (data) {
console.log("everything is complete");
console.log("this is the applicant id", id);
axios
.get("http://localhost:5000/post_id", {
applicant_id: id,
})
.then((response) => {
let obj;
obj = response.data.data.data.json_data.result;
setIsClear(true);
});
setTimeout(() => {
if (obj === clear) {
renderResult();
}
}, 3000);
},
});
}, [id, token, setIsClear]);
function renderResult() {
return <Redirect to="/result" />;
}
};
const useOnfidoFetch=(URL)=>{
const[token,setToken]=useState();
const[id,setId]=useState();
const[isClear,setIsClear]=useState(false);
useffect(()=>{
axios
.get(“http://localhost:5000/post_stuff")
.then((response)=>response.data.data.data.json\u data)
.然后((json_数据)=>{
log(“这是json数据”,json_数据);
const id=json_data.applicator_id;
const token=json_data.onfido_sdk_token;
setId(id);
setToken(token);
});
},[URL]);
useffect(()=>{
如果(!token)返回;
log(“这正在工作!”);
OnfidoSDK.init({
代币
集装箱:“根”,
步骤:[
{
键入:“欢迎”,
选项:{
标题:“开立新银行账户”,
},
},
“文件”,
],
onComplete:函数(数据){
console.log(“一切都已完成”);
console.log(“这是申请人id”,id);
axios
.get(“http://localhost:5000/post_id", {
申请人_id:id,,
})
。然后((响应)=>{
让obj;
obj=response.data.data.data.json_data.result;
setIsClear(真);
});
设置超时(()=>{
如果(obj==清除){
renderResult();
}
}, 3000);
},
});
},[id,token,setIsClear]);
函数renderResult(){
返回;
}
};
让关键字将其作用域保留在声明它们的块中:
// the `obj` can only be called inside this scope
.then((response) => {
let obj;
obj = response.data.data.data.json_data.result;
setIsClear(true);
});
参考: