Write an interface for uploading pictures with nodejs. Using the multer plug-in, I hope to put the function of uploading pictures behind the function of inserting into the database.

I hope to put the function of uploading pictures uploadImage.array ("file", 30) after the function of inserting into the database pool.getConnection . How can I implement

?

short version

app.all("/other/images/save", uploadImage.array("file", 30), (req, res) => {
    pool.getConnection( (err, connection) => {
        //
    })
})

all the code

var storageImage = multer.diskStorage({
    destination: function(req, file, cb) {
        let date = new Date();
        let year = date.getFullYear();
        let month = date.getMonth() + 1;
        let day = date.getDate();
        let loginUserInfo = req.session.loginUserInfo;

        // console.log(" loginUserInfo == ", loginUserInfo);
        if(loginUserInfo !== undefined) {
            let dirPromise = dirExists("./uploads/"+loginUserInfo["uid"]+"/"+year+"_"+month+"_"+day);
            dirPromise.then(v=> {
                // console.log("v === ", v);
                if(v===true) {
                    cb(null, "./uploads/"+loginUserInfo["uid"]+"/"+year+"_"+month+"_"+day);
                } else {
                    cb(null, "./uploads/");
                }
            });
        }
    },
    filename: function(req, file, cb) {
        cb(null, `${Date.now()}-${file.originalname}`)
    }
});


// muler
var uploadImage = multer({ storage: storageImage });

app.all("/other/images/save", uploadImage.array("file", 30), (req, res) => {

    let param = req.body;

    let selectParams = {
        title: param.title,
        userID: 0,
        content: param.content,
        dateline: getCurrentDate(2),
    };

    // console.log(param);
    // 
    let files = req.files;

    // 
    let imageUpload = {};

    if(!files[0]) {
        imageUpload.code = 1;
        imageUpload.message = "fail";
    } else {
        imageUpload.code = 0;
        imageUpload.data = {
            url: files[0].path
        };
        imageUpload.message = "success";
    }
    // res.json(result);

    let insertSql = "INSERT INTO other_message SET ?"; //ID
    let loginUserInfo = {
        "uid": 0
    };


    if (req.session.loginUserInfo !== undefined) {

        loginUserInfo = req.session.loginUserInfo;

        selectParams["userID"] = loginUserInfo["uid"];
        pool.getConnection( (err, connection) => {
            if (err) {
                throw err;
            } else {
                connection.query(
                    insertSql,
                    selectParams,
                    (err,data) => {
                        if(err) {
                            throw err;
                        } else {

                            res.json({
                                "function": "insert",
                                "message": "success",
                                "detail": "",
                                "imageUpload": imageUpload
                            })
                        }
                    }
                )
            }
            pool.releaseConnection(connection);
        });



    } else {
        res.json({
            "function": "insert",
            "message": "fail",
            "detail": "",
            "imageUpload": imageUpload
        })
    }
});
< H1 > reply downstairs, if you change your position, you will report an error < / H1 >


Apr.06,2022

change the location of the middleware

app.all('/other/images/save', (req, res) => {
    pool.getConnection( (err, connection) => {
        //
    })
}, uploadImage.array('file', 30))
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b37aeb-e602.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b37aeb-e602.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?