MongoDB
 sql >> Teknologi Basis Data >  >> NoSQL >> MongoDB

Node js POST Permintaan kesalahan Kesalahan [ERR_HTTP_HEADERS_SENT]:Tidak dapat mengatur header setelah dikirim ke klien

Ini terjadi karena Anda mengirim respons dua kali. Oleh karena itu, kesalahan:

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

Ubah kode Anda menjadi berikut:

router.post('/user', VerifyToken, function (req, res) {
User.findOne({ username: req.body.username }, function (err, user) {
    if (user) return res.status(400).send({status: 'ko', error: { msg: 'The username you have entered is already associated with another user.'}});

    var hashedPassword = bcrypt.hashSync(req.body.password, 8);
    User.create({
        firstname:req.body.firstname,
        surname:req.body.surname,
        username:req.body.username,
        email: req.body.email,
        password: hashedPassword,
        farmId: req.body.farmId,
        roles: req.body.roles,
        isVerified : req.body.isVerified,
        statusUser : req.body.statusUser
        },
        function (err, user) {
            if (err) return res.status(500).send("There was a problem adding the user to the database.");


            var client = nodemailer.createTransport(sgTransport(options));

            var email = {
                from: '[email protected]',
                to: req.body.email,
                subject: 'Registration successfully confirmed',
                text: 'Hi '+ req.body.username + ',\n\nyour account has been registered.\n\nAre you the farm owner?' +
                '\n\nPlease go to this link [CMS link] to create your Profile and start to use the App Plus!\n\n'+
                'If you are a simple user, please just use your credentials to login to the App Plus section into the new App!\n\n'+
                'Download for free from App Store or Google Play!\n\nRegards,\n\nTrelleborg TLC Plus team'
            };

            client.sendMail(email, function(err, json){
                if (err){
                    return res.status(500).send({ msg: err.message });
                }

               res.status(200).send({status: 'ok', data: { msg: 'A verification email has been sent to ' + user.email + '.'}, message: 'User saved.'} )

            });

        });

});

});


  1. Redis
  2.   
  3. MongoDB
  4.   
  5. Memcached
  6.   
  7. HBase
  8.   
  9. CouchDB
  1. Django - MongoDB:(tidak dapat terhubung ke localhost:27017) Koneksi ditolak

  2. Bagaimana cara melakukan pencarian $lookup bersarang di MongoDB?

  3. Proyeksi MongoDB dari Array Bersarang

  4. Nilai unik luwak dalam array objek bersarang

  5. Pencarian teks lengkap di mongodb dalam bahasa Rusia