我的代码的全部范围。我在这里缩小尺寸 Need guidance! Trying to learn about fetch() and Promises尝试使我的问题更简洁。
代码本质上有效。只是当我添加另一个fetch()时,我不知道为什么它不喜欢它
export const addPlace = (placeName, streetName, city, region, postCode, country, location, image, coverImage) => {
return dispatch => {
let authToken;
dispatch(uiStartLoading());
dispatch(authGetToken())
.catch(() => {
alert("No valid token found!");
})
.then(token => {
authToken = token;
return image = fetch("myappURL/storeImage",
{
method: "POST",
body: JSON.stringify({ image: image.base64 }),
headers: {
Authorization: "Bearer " + authToken,
}
}
);
})
.catch(err => {
console.log(err);
alert("Oops! Something went wrong, please try again1")
dispatch(uiStopLoading());
})
.then(res => {
if (res.ok) {
return res.json();
} else {
throw(new Error());
}
})
.then(parsedRes => {console.log(parsedRes);
const placeData = {
name: placeName,
fullAddress: [streetName, city, region, postCode, country],
shortAddress: [city, region],
location: location,
image: parsedRes.imageUrl,
imagePath: parsedRes.imagePath,
coverImage: parsedRes.coverImageUrl, // what I want to add
coverImagePath: parsedRes.coverImagePath // what I want to add
}; // this is the output
return fetch("myappURL/places.json?auth=" + authToken, {
method: "POST",
body: JSON.stringify(placeData)
});
})
.then(res => {
if (res.ok) {
return res.json();
} else {
throw(new Error());
}
})
.then(parsedRes => {
console.log(parsedRes);
dispatch(uiStopLoading());
dispatch(placeAdded());
})
.catch(err => {
console.log(err);
alert("Oops! Something went wrong, please try again2")
dispatch(uiStopLoading());
});
};
};