[SOLVED] Re Render component after an api put request

After updating the MongoDb when recording a vote…

2 possible strategies

  • Call the Api again to get the latest data (some other user may have voted at the same time)
  • Just modify the current state and re-render.

What is the best way do to this?

You asked for best way and I don’t quote know although i think this is addressed in the mongo university course, have you taken it yet?

For simplicity I just retunred the current state of the DB query I think. Does mongoose help with this?

I think it can yes, I will investigate thanks. But it seems like the whole app is not re-rendering the latest view of the DB: after a re-fresh it is still showing the old data.

if you redirect to the poll’s page, your routes should automatically query. That’s what I did.

1 Like

Isn’t a redirect a post back and isn’t that the problem React is trying to solve? (Having to do round trips to the server to update the DOM)

The problem was somewhere in the generic callback functions in the API manager.
The wrong items were being returned to the state.

This is the post method in the API manager

 post: (url, body, callback) => {
        superagent
            .post(url)
            .send(body)
            .set('Accept', 'application/json')
            .end((err, response) => {
                if (err) { 
                	console.log(err);
				    callback(err, null);
				    return;}
				// here check for API failures
				const confirmation = response.body.confirmation;
				if (confirmation != 'success') { // NOT a Success??
					// send a failure message
						console.log('Not a success in *post*');
						callback({message:response.body.message, null});
				    
				    return;
				}
				// Worked!
				callback(null, response.body);
				
            })
        
    },

I havn’t done any react i full stack yet!

1 Like

My solution once I got the API call working correctly was to update state using setState, reflecting the change to the data passed to the database.