Can someone shed light on why I am unable to return a promise and invoke .then() on it?
I am creating an inventory system for my collection of Pokemon cards using a react front-end and an express backend. When I click the "increase inventory" button on the front end, this is the function that calls the API:
incInventory(card, cond) {
// this.setState({currentValue: this.state.currentValue + 1});
const result = fetch('http://mySecretAPIServer/items/' + card._id, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
},
// We convert the React state to JSON and send it as the POST body
body: JSON.stringify({_id: card._id, cond: cond, op: "incQuantity", otherId: card.id})
});
console.log(result);
// ***WHY ISN'T THIS RUNNING?!?!?!?***
result.then((res) => {
console.log("this isn't printing...");
});
}
This is the Express endpoint being accessed:
// UPDATE (Modify)
router.put('/:id', (req, res) => {
const conditionToUpdate = "q" + req.body.cond;
const amtToUpdate = req.body.op === 'incQuantity' ? 1 : -1;
return new Promise((resolve, reject) =>
Pokemon.findByIdAndUpdate(req.params.id, {$inc: {[conditionToUpdate]: amtToUpdate}}, (err, result) => {
resolve(result);
}));
});
As you can see, I am attempting to return a promise from the endpoint. After the fetch call on the front end, here is what the promise looks like before trying to use .then() on it:
https://i.sstatic.net/HvsOQ.png
However, the .then()
function never executes. I don't necessarily need the data from the fetch, I just need to execute some code after the fetch completes.
I hope someone can guide me in figuring out what I may be overlooking! Thank you!