Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

406
Visualizações
NodeJs - Is there a way to put imageToBase64 const inside create method from Sequelize while uploading picture?

I have this route below working fine

routes.post("/product/new", adminAuth, upload.single("picture"), (req,res) => {

    var {title, code, price, amount, supplier } = req.body

    async function convertImage(){
        return await imageToBase64(req.file.path);
    }

    convertImage().then(async(picture) => {
        if(picture){
            console.log(picture)
            Product.create({
                title: title,
                slug: slugify(title),
                code: code,
                price: price,
                amount: amount,
                // picture: picture,
                supplierId: supplier
            }).then(() =>{
                res.redirect("/admin/products");
            });
        }
    }).catch(err => {
        throw err
    });
});

However, when I pass the variable picture it shows up the following log message:

UnhandledPromiseRejectionWarning: Error
UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1 (node:9735) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

My questions is related to Sequelize method create.({}), can I pass the const from imageToBase64 inside? The idea is to convert the picture uploaded to base64 and saving it in a database.

Here is the Product model

const Product = connection.define('products', {
title:{
    type: Sequelize.STRING,
    allowNull: false
},
slug: {
    type: Sequelize.STRING,
    allowNull: false
},
code: {
    type: Sequelize.STRING,
    allowNull: false
},
price: {
    type: Sequelize.STRING,
    allowNull: true
},
amount: {
    type: Sequelize.STRING,
    allowNull: true
},
picture: {
    type: Sequelize.STRING,
    allowNull: true
}});
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

try BLOB

 picture:  {
     type: DataTypes.BLOB('long'),
     get () {
         let data = this.getDataValue('picture');
         return data ? data.toString('base64') : '';
     }
 }
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda