ฉันมีการดำเนินการที่จะบันทึกโทเค็น JWT ด้วยการเรียกง่ายๆว่า "saveJwt (data)"
นี่คือการดำเนินการ:
export const requestLoginToken = (username, password) =>
(dispatch, getState) => {
dispatch({type: REQUEST_LOGIN_TOKEN, payload: username})
const payload = {
userName: username,
password: password,
}
const task = fetch('/api/jwt', {
method: 'POST',
body: JSON.stringify(payload),
headers: {
'Content-Type': 'application/json;charset=UTF-8'
},
})
.then(handleErrors)
.then(response => response.json())
.then(data => {
dispatch({type: RECEIVE_LOGIN_TOKEN, payload: data})
saveJwt(data)
})
.catch(error => {
clearJwt()
dispatch({type: ERROR_LOGIN_TOKEN, payload: error.message})
})
addTask(task)
return task
}
จากนั้นฉันก็เพิ่มเข้าไปภายใต้ "saveJwt(data)" บล็อกโค้ดนี้:
if (!confirmSelectDataExistance()) {
dispatch({ type: REQUEST_SELECT_DATA })
const token = getJwt()
const headers = new Headers({
'Authorization': `Bearer ${token}`
})
const retrieveSelectData = fetch('/api/SelectData/SelectData', {
method: 'GET',
headers: {
'Content-Type': 'application/json;charset=UTF-8'
},
})
.then(handleErrors)
.then(response => response.json())
.then(selectData => {
dispatch({ type: RECEIVE_SELECT_DATA, payload: selectData })
saveSelectData(selectData)
})
}
สิ่งที่ต้องทำคือตรวจสอบว่ารายการอยู่ในที่จัดเก็บในตัวเครื่องหรือไม่ และหากเป็นเช่นนั้น ให้ทำการดึงข้อมูลอีกครั้งโดยเพิ่ม JWT
นี่คือการดำเนินการที่เสร็จสมบูรณ์เพื่อให้คุณเห็นว่ามันอยู่ที่ไหน:
export const requestLoginToken = (username, password) =>
(dispatch, getState) => {
dispatch({ type: REQUEST_LOGIN_TOKEN, payload: username })
const payload = {
userName: username,
password: password,
}
const task = fetch('/api/jwt', {
method: 'POST',
body: JSON.stringify(payload),
headers: {
'Content-Type': 'application/json;charset=UTF-8'
},
})
.then(handleErrors)
.then(response => response.json())
.then(data => {
dispatch({ type: RECEIVE_LOGIN_TOKEN, payload: data })
saveJwt(data)
//selectData download if nothing is local storage.
if (!confirmSelectDataExistance()) {
dispatch({ type: REQUEST_SELECT_DATA })
const token = getJwt()
const headers = new Headers({
'Authorization': `Bearer ${token}`
})
const retrieveSelectData = fetch('/api/SelectData/SelectData', {
method: 'GET',
headers: {
'Content-Type': 'application/json;charset=UTF-8'
},
})
.then(handleErrors)
.then(response => response.json())
.then(selectData => {
dispatch({ type: RECEIVE_SELECT_DATA, payload: selectData })
saveSelectData(selectData)
})
}
})
.catch(error => {
clearJwt()
dispatch({ type: ERROR_LOGIN_TOKEN, payload: error.message })
})
addTask(task)
return task
}
ดังนั้นตอนนี้การดึงข้อมูลครั้งแรกของฉันจบลงด้วยสัญญาที่ไม่ได้กำหนดไว้และมันก็กระโดดลงไปที่ด้านล่างโดยไม่ได้กำหนดมูลค่าของงาน
คุณจะเชื่อไหมว่าเมื่อวานมันทำงานได้อย่างสมบูรณ์แบบ และเมื่อฉันลองวันนี้มันก็ล้มเหลว
เหตุใดการบล็อกโค้ดนั้นทำให้ทุกอย่างล้มเหลว .. ฉันแสดงความคิดเห็นและมันก็ใช้ได้ดี ..
ฉันสงสัยว่ามันจะเกี่ยวข้องกับสัญญาสองข้อ ฯลฯ แต่จะต้องมีวิธีที่ถูกต้องในการจัดการสิ่งนี้ .. ฉันคิดว่าถ้ามันจะปิดลงเพื่อบันทึกโทเค็น JWT นั่นจะบ่งบอกว่ามันเสร็จสิ้นด้วยการดาวน์โหลดครั้งแรก แต่มีบางอย่าง อย่างอื่นผิด... ในบล็อกของโค้ดนั้นหรือมีแนวโน้มว่าจะมีการใช้อย่างไร