Home > Software engineering >  Malformed Array Literal postgresql
Malformed Array Literal postgresql

Time:07-25

I would like to insert data in the form of a string in a column of type array (char[]) only my request returns the error "Malformed Array Literal"

here is my request:

async function insertTag(req, res) {
    return db.none('UPDATE user_account SET tag_property = $1 WHERE email = $2',
            [req.body.tag_id, req.body.email])
        .then(function () {
            res.status(200)
                .json({
                    status: 'success',
                    message: 'Updated user',
                });
        })
        .catch(error => {
            console.log(error)
        });
}

here is my data table enter image description here

tagIdInput: '',
InsertTagUserAccount(){
                fetch('http://192.168.1.51:3000/api/v1/tag_insertion', {
                    method: 'PUT',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({
                        email : '[email protected]',
                        tag_id : '{'   this.tagIdInput   '}',
                    }),
                })
                // Converting to JSON
                .then(response => response.json())
                // Displaying results to console
                .then(json => console.log(json));       
                },

How can I fix that ?

CodePudding user response:

I don't now how to add a new tag_id instead of replacing the old one

See modifying arrays in the docs and use the array_append function for that:

UPDATE user_account
SET tag_property = array_append(tag_property, $1)
WHERE email = $2;

If your frontend code is actually sending an array of multiple items to add, you can use

UPDATE user_account
SET tag_property = tag_property || $1::text[]
WHERE email = $2;
  • Related