Can't set headers after they are sent when using dbquery multiple times

can definition
can synonym
can verb
can y
can noun meaning
can band
cant
can't

I get this error #Can't set headers after they are sent# when using dbquery multiple times.

Hi, I'm almost new to node.js and I can't figure out why this error appears. If I try with one dbquery then everything is ok. But if I use multiple query then it crashes.

router.get(
  '/auth/facebook/token',
  (req, res, next) => {
    passport.authenticate('facebook-token', (error, user, info) => {

                  if (user){
                    myDB.setUser(user.email,(err,pinfo) =>{
                      if (err) {
                       res.send(err);
                     }
                     else {
                       res.send(pinfo);    <- Crash at this line!!!      
                     }
                 });
            } 
      next();
    })(req,res, next);
  }
);




function setUser (email,  cb) {

  pool.query("INSERT INTO users (email,pinfo) VALUES (?,0)", email, (err, result) => {
    if(err){
      cb(err);
      return;
     }
    else{
      pool.query("SELECT pinfo FROM users WHERE email = ?", email,(err,pinfo) =>{
        cb(err,pinfo);
      });
    }
  }); 
}

You are calling next middle layer function using next(). After sending the response to the user. Try without next or modify your logic to. Hope this will help you

Can, Can may refer to: Contents. 1 Containers; 2 Music; 3 Other; 4 See also. Containers[edit]. Aluminum can · Drink can · Oil can · Steel and tin cans · Trash can  Can definition, to be able to; have the ability, power, or skill to: She can solve the problem easily, I'm sure. See more.

once you are used res.send you cannot use it again

it seems you are sending a response at two positions please check the logic thoroughly

The Official CAN / Spoon Records Website, VerbEdit. can (third-person singular simple present can, present participle -, simple past could, past participle (obsolete except in adjectival use) couth). Define can. can synonyms, can pronunciation, can translation, English dictionary definition of can. to be able to, have the power or skill to: I can take a bus to the

Some Where you sending response twice and hence this error. R.sarkar is right take out next() call and add it somewhere you will want to continue with your next dbquery or else respond from top function only once and never call next().

Can, can meaning: 1. to be able to: 2. used to say that you can and will do something: 3. to be allowed to: . Learn more. 110 synonyms of can from the Merriam-Webster Thesaurus, plus 97 related words, definitions, and antonyms. Find another word for can. Can: to bring (as an action or operation) to an immediate end.

can, 1.4Used to indicate that something is typically the case. 'antique clocks can seem out of place in modern homes'. More example sentences. Can means someone or something knows how to, is able to, is likely to or has the right to do something. An example of can is someone knowing how to play the piano. An example of can is a cat being able to paint. An example of can is a car that usually starts.

CAN, can. (kæn). n. 1. a container, esp for liquids, usually of thin sheet metal: a petrol can;  In negative constructions, can't or cannot is more common than may not; the contraction mayn't is rare: You can't park in the driveway. can but and cannot but are formal and old-fashioned expressions suggesting that there is no other way to do something. can2 /kæn/ n., v., canned, can•ning.

Can, Can-Am offers a complete lineup of Off-Road vehicles (ATV, Side-by-Side) and 3-​wheel motorcycles (Spyder or Ryker). Choose your playground now and enjoy  Looking for online definition of CAN or what CAN stands for? CAN is listed in the World's largest and most authoritative dictionary database of abbreviations and acronyms The Free Dictionary

Comments
  • why there is (req, res, next) at the end of passport.auth function?
  • I copied that part from somewhere without understanding why and if I remove that part code doesn't even function.
  • Thanks! Node.js program logic is quite unfamiliar but interesting.
  • if you are using express. Try to understand middle layer concept. If this answer was useful marked it as useful, so that it might help others.
  • Yeah, but I can't find where it went wrong. After first query starts in setUser function, it calls next() in router.get. And then it gets into the setUser function again to check if there is an error, and then starts second query, and if no error, it starts res.send(pinfo) which causes the error.
  • Problem solved! I moved next() into if error checker and no error appears. I'll have to learn about next function. Thanks!!