Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
82 views
in Technique[技术] by (71.8m points)

node.js - Update a column in database in NodeJs

I want to update a column which has a relation:

async update(req, res): Promise<any> {
        const {email, password, id} = req.body;
        try {
            await getConnection()
                .createQueryBuilder()
                .update(User)
                .set({
                    email: email,
                    password: password,
                    meta: [{   
                        name: "tesst",
                        message: "jasshd"
                    }]
                })
                .where("id = :id", {id: id})
                .execute();

            res.status(201).send({
                message: 'user updated'
            })

        } catch (e) {
            console.log('update', e)
            res.send({
                message: 'update error'
            })
        }
    }
question from:https://stackoverflow.com/questions/65907844/update-a-column-in-database-in-nodejs

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I don't think you can update the relation data with querybuilder, the solution I see here is to update meta with itself :

async update(req, res): Promise<any> {
    const {email, password, id} = req.body;
    try {
        await getConnection()
            .createQueryBuilder()
            .update(User)
            .set({
                email: email,
                password: password,
                meta: [{   
                    name: "tesst",
                    message: "jasshd"
                }]
            })
            .where("id = :id", {id: id})
            .execute();
  // Update meta :
    await getConnection()
            .createQueryBuilder()
            .update(Meta)
            .set({ 
                    name: "tesst",
                    message: "jasshd"
            })
            .where("user = :id", {id: id})
            .execute();
        res.status(201).send({
            message: 'user updated'
        })

    } catch (e) {
        console.log('update', e)
        res.send({
            message: 'update error'
        })
    }
}

Ps: this operation will update all the data of meta that has user = :id


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...