Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
275 views
in Technique[技术] by (71.8m points)

node.js - Cannot set headers after they are sent to the client NODEJS MONGOOSE

i have these post on express with nodejs but it throws this error:

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

these error was catched by the try/catch of the newProducto.save() but i dont understand what is happening

route.post('/post/subiruno', (req, res) => {
    ProductoModel.findOne({
        nombre: req.body.nombre
    }).then(
        (producto) => {
            if (producto) {
                console.log(`El producto ${req.body.nombre} ya existe`)
                return res.status(400).json({
                    message: `El producto ${req.body.nombre} ya existe`
                })
            } else {
                let newProducto = new ProductoModel({
                    nombre: req.body.nombre,
                    marca: req.body.marca,
                    cantidad: req.body.cantidad,
                    distribuidor: req.body.distribuidor,
                    unidad_De_Empaque: req.body.unidad_De_Empaque,
                    valor_Unidad_De_Empaque: req.body.valor_Unidad_De_Empaque,
                    valor_Unitario: req.body.valor_Unitario,
                    distribuidor_2: req.body.distribuidor,
                    unidad_De_Empaque_2: req.body.unidad_De_Empaque,
                    valor_Unidad_De_Empaque_2: req.body.valor_Unidad_De_Empaque,
                    valor_Unitario_2: req.body.valor_Unitario,
                    distribuidor_3: req.body.distribuidor,
                    unidad_De_Empaque_3: req.body.unidad_De_Empaque,
                    valor_Unidad_De_Empaque_3: req.body.valor_Unidad_De_Empaque,
                    valor_Unitario_3: req.body.valor_Unitario,
                    categoria: req.body.categoria,
                    subCategorias: req.body.subCategoria
                })

                newProducto.save()
                    .then(
                        res.json(newProducto)
                    ).catch(err => {
                        res.json(err)
                    })
            }
        }
    )
})
question from:https://stackoverflow.com/questions/65648458/cannot-set-headers-after-they-are-sent-to-the-client-nodejs-mongoose

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As said, @jfriend00 the mistake was on the .then This is the code corrected. A big hug people!

route.post('/post/subiruno', (req, res) => {
    ProductoModel.findOne({
        nombre: req.body.nombre
    }).then(
        (producto) => {
            if (producto) {
                console.log(`El producto ${req.body.nombre} ya existe`)
                return res.status(400).json({
                    message: `El producto ${req.body.nombre} ya existe`
                })
            } else {

                const generadorDeCodigo = (categoria) => {
                    const tipo = categoria;
                    const tipoSliced = tipo.slice(0, 3);
                    var codigo = `${tipoSliced}`;
                    return codigo
                }

                let newProducto = new ProductoModel({
                    nombre: req.body.nombre,
                    marca: req.body.marca,
                    cantidad: req.body.cantidad,
                    distribuidor: req.body.distribuidor,
                    unidad_De_Empaque: req.body.unidad_De_Empaque,
                    valor_Unidad_De_Empaque: req.body.valor_Unidad_De_Empaque,
                    valor_Unitario: req.body.valor_Unitario,
                    distribuidor_2: req.body.distribuidor,
                    unidad_De_Empaque_2: req.body.unidad_De_Empaque,
                    valor_Unidad_De_Empaque_2: req.body.valor_Unidad_De_Empaque,
                    valor_Unitario_2: req.body.valor_Unitario,
                    distribuidor_3: req.body.distribuidor,
                    unidad_De_Empaque_3: req.body.unidad_De_Empaque,
                    valor_Unidad_De_Empaque_3: req.body.valor_Unidad_De_Empaque,
                    valor_Unitario_3: req.body.valor_Unitario,
                    categoria: req.body.categoria,
                    subCategorias: req.body.subCategoria,
                    codigoDeProducto: generadorDeCodigo(req.body.categoria)
                })

                newProducto.save()
                    .then(() => {
                        res.json(newProducto)
                    }
                    ).catch(err => {
                        res.json(err)
                    })
            }
        }
    )
})

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...