我有以下useEffect回调函数:
import { initDB, useIndexedDB } from "react-indexed-db";
initDB(DBConfig);
const db = useIndexedDB("reads");
useEffect(() => {
db.getByIndex("hash", props.match.params.id).then((data) => {
setToken(data.token);
});
handleTextColorSelection(backgroundColor);
axios
.post(`${process.env.REACT_APP_API_URL}khatma/read`, {
khatma: props.match.params.id,
part: props.match.params.part,
section: props.match.params.section,
token: token,
})
.then((res) => {
if (res.data.token) {
db.add({
hash: props.match.params.id,
token: res.data.token,
}).then(
(event) => {},
(error) => {
console.log(error);
}
);
}
})在axios post body中,我正在发送令牌,如果" token“状态接收到之前存储在浏览器索引数据库中的值,如果没有找到对象存储数据,则post令牌将作为null发送。
这里的问题是,我注意到在db.getByIndex(“散列”,...命令获得一个结果,运行一个axios请求,并将一个令牌作为null发送,尽管稍后令牌状态将获得一个值。
我如何运行db.getByIndex(“散列”,如果它完成了,运行axios post请求?
发布于 2020-06-30 03:31:05
您可以使用.then
const axiosRequest = () => { /* your axios logic */ };
db.getByIndex(...).then(axiosRequest).catch(axiosRequest)您在then和catch回调中所做的任何操作都将在getByIndex调用之后运行
https://stackoverflow.com/questions/62645236
复制相似问题