'How to check whether a user exists before posting a data through axios.post method
I am creating a simple app using vue.js to control mail delivery and i would like to achieve the following: Before i send a data through axios post method coming from the v-model i need to check first,whether the apartment already exists and if it exists I will just add the information of the mail otherwise, i will post the whole data including the name, id, apartment and the mail field info that happens to be an object.
Here is my json.server data
{
"blocoA": [
{
"name": "michele ",
"apartament": "100",
"mail": [
{
"code": "3445566775677",
"quantity": 1,
"date_recieved":"31/01/2022"
}
],
"id": 1
},
{
"name": "becky",
"apartament": "10",
"mail": [
{
"code":"6789900099876655"
"quantity": 2
"date_recieved":"25/01/2022"
}
],
"id": 2
}
],
This is my axios method:
const baseURL = "http://localhost:3000/blocoA";
async addMail() {
try {
//const res =
await axios.post(baseURL,
{ name: this.name,
apartament: this.apart,
mail: [{
code:this.code
quantity:this.mail.quant
date_received:this.date
}]
}).then(()=>{
this.isSuccess = true
});
ok, the help i need is when the addMail() function is called i would like to check if the apartment already exists in the database and if it does then i will add up only the infomations of the mail field objects otherwise i will post all the data. I want to avoid duplicating apartment when they already exist.
The desired result :
{
"blocoA": [
{
"name": "michele ",
"apartament": "100",
"mail": [
{
"code": "3445566775677",
"quantity": 1,
"date_recieved":"10/01/2022"
},
{
"code": "99999999999999999",
"quantity": 1,
"date_recieved":"13/01/2022"
}
],
"id": 1
},
NB: Now I have a list of objects in the mail field and didn't duplicate the apartment for the same Tenant and avoided having different IDs too.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
