简体   繁体   中英

ReactJS MySQL update if value is not null/empty

I'm a beginner in react js and I'm trying to update password and image fields only if they are not empty values coming from the frontend based on this answer but I'm getting the following error:

Error: Incorrect arguments to mysqld_stmt_execute

node.js script:

async updateUser(req, res) {

        const { id } = req.params;

        const {
            fullname,
            CPF,
            RG,
            born,
            email,
            phone,
            password,
            level, 
            city, 
            cityHallState } = req.body;

        let image;
        if (req.files.length > 0) {
            image = req.files[0].filename;
        } else {
            image = '';
        }

        let created = new Date();

        let date = ("0" + created.getDate()).slice(-2);
        let month = ("0" + (created.getMonth() + 1)).slice(-2);
        let year = created.getFullYear();
        let hours = created.getHours();
        let minutes = created.getMinutes();
        let seconds = created.getSeconds();

        let updatedAt = year + "-" + month + "-" + date + " " + hours + ":" + minutes + ":" + seconds;

        try {

            await conn.execute('UPDATE register SET fullname = ?, CPF = ?, RG = ?, born = ?, email = ?, phone = ?, city = ?, state = ?, password = CASE WHEN ? IS NOT NULL AND LENGTH(?) > 0 THEN ? ELSE password END, image = CASE WHEN ? IS NOT NULL AND LENGTH(?) > 0 THEN ? ELSE image END, level = ?, updated = ? WHERE id = ?',
                [fullname, CPF, RG, born, email, phone, city, cityHallState, password, image, level, updatedAt, id],
                function (err, results, fields) {

                    console.log('register errors: ' + err);
                    console.log(results);

                    if (err == null) {
                        if (results.affectedRows > 0) {
                            return res.json('user updated');
                        } else {
                            return res.json('could not update user data');
                        }
                    } else {
                        return res.json('error updating user data: ' + err);
                    }
                });
        } catch (error) {
            return res.json('could not update user data. catch error: ' + error);
        }
}

If I remove password and image fields the query executes fine. What am I doing wrong?

Are you trying to set the defaults to the string 'password' or 'image' if the lengths are greater than 0? If so, you will probably need single quotes around those:

UPDATE register
SET fullname = ?,
    CPF = ?,
    RG = ?,
    born = ?,
    email = ?,
    phone = ?,
    city = ?,
    state = ?,
    password = CASE WHEN ? IS NOT NULL AND LENGTH(?) > 0 THEN ? ELSE 'password' END,
    image = CASE WHEN ? IS NOT NULL AND LENGTH(?) > 0 THEN ? ELSE 'image' END,
    level = ?,
    updated = ?
WHERE id = ?'

You can also clean up the query by removing the case statements altogether and setting a default for both password and image in javascript:

async updateUser(req, res) {

        const { id } = req.params;

        const {
            fullname,
            CPF,
            RG,
            born,
            email,
            phone,
            password = 'password',
            level, 
            city, 
            cityHallState } = req.body;

        let image = 'image';
        if (req.files.length > 0) {
          image = req.files[0].filename;
        }

        ...

        try {

          await conn.execute('UPDATE register SET fullname = ?, CPF = ?, RG = ?, born = ?, email = ?, phone = ?, city = ?, state = ?, password = ?, image = ?, level = ?, updated = ? WHERE id = ?'
        ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM