nodejs-是否有一种方法可以将ImagetObase64 const放置在上传图片时续集中的创建方法中?

发布于 2025-01-27 23:49:33 字数 2059 浏览 3 评论 0原文

但是,我有以下工作正常工作的路线

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
    });
});

,但是,当我传递变量图片时,它显示以下日志消息:

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.

我的问题与续集方法相关。({})可以从ImagetObase64内部?这个想法是将上传到base64的图片将其保存在数据库中。

这是产品模型

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
}});

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
}});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感性不性感 2025-02-03 23:49:33

尝试斑点

 picture:  {
     type: DataTypes.BLOB('long'),
     get () {
         let data = this.getDataValue('picture');
         return data ? data.toString('base64') : '';
     }
 }

try BLOB

 picture:  {
     type: DataTypes.BLOB('long'),
     get () {
         let data = this.getDataValue('picture');
         return data ? data.toString('base64') : '';
     }
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文