Вот мой соответствующий код:
Код: Выделить всё
# Cast vote
@router.post("/api/poll")
async def cast_vote(vote, ip):
if poll_results_collection:
alreadyVoted = poll_results_collection.find_one({"ip": ip})
if alreadyVoted:
return {"status": "already_voted"}
poll_results_collection.insert_one({"vote": vote, "ip": ip})
return {"status": "success"}
return {"status": "Failed to connect"}
Код: Выделить всё
async handleSubmit() {
this.state = 'submitting';
const ip = await this.getIp();
const url = `${config.baseURL}/poll`
try {
const res = await axios.post(url, this.selectedIndex, ip);
this.responseMessage = res.data.status === 'success' ? 'Your vote has been submitted!' : 'You have already voted.';
} catch (e) {
console.error('Error submitting vote', e);
this.responseMessage = 'Failed to submit your vote. Please try again later.';
} finally {
this.state = 'voted';
}
},
Подробнее здесь: https://stackoverflow.com/questions/789 ... ing-python