Getting the query does not get the latest data added using the publication

advertisements

I am trying to use the data that I just added using Post, I send a get request to get all the data in the table, but it does not get the last data added is there a specific logic I should follow to get the updated data ?

Here is my controller :

app.ajoutProjet= function () {
  // adding a new project (CreatProjet() is a service that uses Post to create a new project)
 Projet.createProjet(app.ajoutData);
  // getting all the projects
 Projet.getProjet().then(function(result){
  for(var i=0;i<result.data.projetsListe.length;i++){
      //Enters this condition if the name of the project is the one we just added
    if(result.data.projetsListe[i].NomProjet==app.ajoutData.NomProjet){
      app.ajoutData.projet_id=result.data.projetsListe[i].IdProjet;
      CP.createCP(app.ajoutData);
     }
   }
 }); app.successMsg = 'Project added...Redirection';

EDIT :

Here is the Project Service :

//create a project
     createProjet : function (ajoutData) {
     return $http.post('/api/projets',ajoutData);
     },

 //get all projects
     getProjet : function () {
     return $http.get('/api/projets');
     }

So the project is created, but the get request doesn't return it in the list of projects, any suggestions ?

EDIT

Here is my Post method details :

router.post('/projets',function(req , res){
projet.Projet.sync({force: false}).then(function () {
  // Table created
 return projet.Projet.create({
     IdProjet : req.body.IdProjet,
     NomProjet: req.body.NomProjet,
     ResponsableApitech: req.body.ResponsableApitech,
     ResponsableClient: req.body.ResponsableClient,
     client_id: req.body.client_id,
     estArchive: req.body.estArchive

});

  });
});


This occurs when you call the get request before the post request finished (while it is still processing). you need to wait until the post request is finished and then call the get request. For that, you can use promise chain mechanism

Projet.createProjet(app.ajoutData).then(function(response){
  return Projet.getProjet();
})
.then(function(result){
  for(var i=0;i<result.data.projetsListe.length;i++){
      //Enters this condition if the name of the project is the one we just added
    if(result.data.projetsListe[i].NomProjet==app.ajoutData.NomProjet){
      app.ajoutData.projet_id=result.data.projetsListe[i].IdProjet;
      CP.createCP(app.ajoutData);
     }
   }
});