I'm asking this question as I cannot find an issue like this anywhere online.
I post an update to a mongodb database from a React application to update an objects x and y position, or the components ('widgets') within the object.
This works fine for 6 post calls to 'updatePage' to the database to either add or remove widgets or move the page. But after the 6th call it no longer accepts any posts to the database (React state will continue to change, but it is as if the router no longer accepts any incoming information).
Is there a limit setting somewhere on the number of consecutive posts which can be made in node that I don't know about? Or is this an issue with my code? I find it unusual that I never get a response call from the node router (whether the post method was successful or not...).
Here is the code to call the post update to the router from the React side:
function saveUpdatedPage (update, id) {
console.log("updating DB for ",update)
Api.withToken().post('/pageupdate/' id,
update
).then(function (response) {
console.log("post function returned: ",response.data)
}).catch(function (error) {
console.log("page save failed for some reason: ",error.response);
});
}
Here is the post method within the router:
router.post('/pageupdate/:_id',auth, async(req,res)=>{
console.log("received request to update: ",req.body," id ",req.params);
const filter = { "_id": req.params };
const update = [{ $set: req.body }];
let updatedDoc = await Pages.findOneAndUpdate(filter, update, {returnNewDocument: true})
});
Here are the express header settings etc... if they help at all
const app = express()
app.use(express.json());
app.use(express.urlencoded({extended: true}));
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
next();
});
app.use(userRouter)
app.use(pageRouter)
app.use(express.static(path.join(__dirname, '../front/build')));
Here is the console after moving a page 7 times (6 successful posts to the database and thereafter no calls will be successful)
React console showing page state and database calls sent
Node console ceasing to log any posts after the 6th post
CodePudding user response:
I am an idiot. For some reason I had left the try and catch block out of the post method in the router. That is why it wasn't giving any response from the router!
I have no idea why the database updates 6 times successfully without a try/catch block. But adding in the try catch and response allows it to update endlessly.
(see updated router post method with try and catch below)
router.post('/pageupdate/:_id',auth, async(req,res)=>{
console.log("received request to update: ",req.body," id ",req.params);
const filter = { "_id": req.params };
const update = [{ $set: req.body }];
try{
let updatedDoc = await Pages.findOneAndUpdate(filter, update, {returnNewDocument: true});
res.status(201).send(updatedDoc)
}
catch(e){
console.log(e);
res.status(400).send(e)
}
});
CodePudding user response:
Why does axios post method stop working after 6 consecutive post calls?
It is probably because your original code was not sending any response back to the client. A browser has limits on how many requests it will allow in progress to the same host. Once it hits that limit, it queues the request and waits until one of the previous requests finishes before sending the queued request.
So, once you added res.send(...)
to your POST handler (for both success and error conditions), then the browser didn't hit its simultaneous requests limit to the same host and you could send more requests just fine.