Express. js [ERR_HTTP_HEADERS_SENT] - PullRequest
       8

Express. js [ERR_HTTP_HEADERS_SENT]

0 голосов
/ 13 апреля 2020

Не могу понять, почему мой второй запрос mongodb выполняется до того, как мой первый запрос будет выполнен. Я попытался объединить первый запрос с помощью then () и async / await aswell.

exports.signup = (req, res, next) => {                                         
  const username = req.body.username;                                          
  const password = req.body.password;                                          

  User.find({}, (err) => {                                                     
  ¦ if (err) {                                                                 
  ¦ ¦ return next(err);                                                        
  ¦ }                                                                          
  ¦ User.countDocuments({}, (err, count) => {                                  
  ¦ ¦ if (err) {                                                               
  ¦ ¦ ¦ return next(err);                                                      
  ¦ ¦ }                                                                        
  ¦ ¦ if (count >= 10) {                                                       
  ¦ ¦ ¦ return res.status(400).send({ error: "Max amount of users registred" })
  ¦ ¦ }                                                                        
  ¦ });                                                                        
  });                                                                          

  User.findOne({ username: username }, (err, existingUser) => {                
  ¦ if (err) {                                                                 
  ¦ ¦ return next(err);                                                        
  ¦ }                                                                          

  ¦ if (!username || !password) {                                              
  ¦ ¦ return res                                                               
  ¦ ¦ ¦ .status(422)                                                           
  ¦ ¦ ¦ .send({ error: "You must provide username and password" });            
  ¦ }                                                                          

  ¦ if (existingUser) {                                                        
  ¦ ¦ return res.status(422).send({ error: "Username in use" });               
  ¦ }                                                                          

  ¦ const user = new User({                                                    
  ¦ ¦ username: username,                                                      
  ¦ ¦ password: password,                                                      
  ¦ });                                                                        

  ¦ user.save((err) => {                                                       
  ¦ ¦ if (err) {                                                               
  ¦ ¦ ¦ return next(err);                                                      
  ¦ ¦ }                                                                        
  ¦ ¦ res.send({                                                               
  ¦ ¦ ¦ token: tokenForUser(user),                                             
  ¦ ¦ ¦ user: { _id: user._id, username: user.username },                      
  ¦ ¦ });                                                                      
  ¦ });                                                                        
  });                                                                          
};                                                                             

Я попытался войти в оператор if (count> = 10), и он выполняется после того, как я уже вошел в Блок user.findOne ({})

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...