Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

132
Views
Post function running forever in JS

I have a function setup to basically check if someone is signed in and if they are adding 1 to a variable. The problem is when run in an application such as a postman it lasts forever, and when to run in the browser it never goes to .then.

Html

$('.fa-regular.fa-heart').click(function(){
        $regular.toggleClass('hide');
        $solid.toggleClass('hide');
        $added.toggleClass('hide');
        $noadded.toggleClass('hide');
        console.log("running");
        fetch("/articles/popplus/<%= article.id%>", {
            method: 'POST',
            mode: 'cors',
            cache: 'default',
            body: JSON.stringify(null)
        }).then((data) => {
            console.log("data")
            console.log(data)
        });
    })

Node.js

router.post('/popplus/:id', async (req, res, next) => {
  var id = req.params.id;
  const user = await Users.findOne({ username: req.session.username })
  console.log(user)
  if (user){
    console.log(user.likedposts)
    if (!user.likedposts.includes(id)){
      console.log("here")
      req.article = await Article.findById(id)
      req.article.pop += 1
      let article = req.article
      user.likedposts.push(id)
      await user.save()
      await article.save()
      return "done"
    }
    else{
      console.log("already liked this post")
      return "already liked this post";
    }
  }
  else{
    console.log("sighn in");
    return {test: "must sighn in to like posts"};
  }
})
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

To complete an Express request handler, you MUST send some sort of http response. The usual way to do that is with res.send(...) or res.json(...), but if you have no data to send, you can also just do res.sendStatus(xxx) or even just res.end().

Express does not pay any attention to what you return from your request handler so returning a value from the request handler does nothing at all - it is entirely ignored. Instead, you must explicitly call one of the above methods to send an http response.

If you don't send an http response by calling one of these type Express methods on the response object, then the client just sits there waiting for a response to come back to its request and it waits for awhile until eventually it times out the request.

over 4 years ago · Santiago Trujillo Report

0

Use res.send() instead of return becasue return is not used for event handlers.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!