1 回答

TA貢獻(xiàn)1797條經(jīng)驗(yàn) 獲得超4個(gè)贊
無需將整個(gè)對(duì)象轉(zhuǎn)換為字符串。因?yàn)樽址瘜?duì)象將返回結(jié)果JSON.stringify(),就像"{key: val}"
要實(shí)現(xiàn)當(dāng)前任務(wù),您可以直接將變量解析為整數(shù),但請(qǐng)確保您希望它為整數(shù)而不是浮點(diǎn)數(shù)。
因此,如果這樣編寫,您的代碼可能會(huì)起作用:
export const fetchById= (id) =>dispatch =>{
//id: object
const i= parseInt(id); //number
console.log(typeof id); //number
fetch(`https://cors-anywhere.herokuapp.com/https://api.nasa.gov/neo/rest/v1/neo?asteroid_id=${i}&api_key=demo_key`)
.then(res => res.json())
.then(data =>{
// const d= {...data.near_earth_objects};
return dispatch({
type: actions.fetchByIds, data: data
});
});
順便說一句,將其轉(zhuǎn)換為 int 是沒有用的,因?yàn)槟龅闹皇窃诓樵冎惺褂盟?,并且查詢將被轉(zhuǎn)換為字符串,如果您asteroid_id在服務(wù)器中記錄類型,它將是字符串。
添加回答
舉報(bào)