Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 在NodeJs中更新数据库中的列_Node.js_Nestjs - Fatal编程技术网

Node.js 在NodeJs中更新数据库中的列

Node.js 在NodeJs中更新数据库中的列,node.js,nestjs,Node.js,Nestjs,我想更新具有以下关系的列: 异步更新(req,res):承诺{ const{email,password,id}=req.body; 试一试{ 等待getConnection() .createQueryBuilder() .更新(用户) .设置({ 电邮:电邮,, 密码:密码, 元:[{ 姓名:“苔丝”, 信息:“jasshd” }] }) .where(“id=:id”,{id:id}) .execute(); 资源状态(201)。发送({ 消息:“用户已更新” }) }捕获(e){ co

我想更新具有以下关系的列:

异步更新(req,res):承诺{ const{email,password,id}=req.body; 试一试{ 等待getConnection() .createQueryBuilder() .更新(用户) .设置({ 电邮:电邮,, 密码:密码, 元:[{ 姓名:“苔丝”, 信息:“jasshd” }] }) .where(“id=:id”,{id:id}) .execute(); 资源状态(201)。发送({ 消息:“用户已更新” }) }捕获(e){ console.log('update',e) res.send({ 消息:“更新错误” }) }
}我认为您无法使用querybuilder更新关系数据,我在这里看到的解决方案是使用自身更新
meta

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'
        })
    }
}
异步更新(req,res):承诺{ const{email,password,id}=req.body; 试一试{ 等待getConnection() .createQueryBuilder() .更新(用户) .设置({ 电邮:电邮,, 密码:密码, 元:[{ 姓名:“苔丝”, 信息:“jasshd” }] }) .where(“id=:id”,{id:id}) .execute(); //更新元数据: 等待getConnection() .createQueryBuilder() .更新(元) .set({ 姓名:“苔丝”, 信息:“jasshd” }) .where(“user=:id”,{id:id}) .execute(); 资源状态(201)。发送({ 消息:“用户已更新” }) }捕获(e){ console.log('update',e) res.send({ 消息:“更新错误” }) } }
Ps:此操作将更新所有具有
user=:id

的meta数据。您可以提供meta和user实体以查看它们之间的关系吗?@Youba one tomany@Youba我更新了两个实体的问题。你能帮忙吗?我认为你不能从用户QueryBuilder更新关系数据,解决方案是创建另一个元QueryBuilder来更新他们的数据,我可以提供一个例子,你对此持肯定态度solution@Youba,这会有帮助的。你能告诉我如何更改我的代码吗?这就是你想要的吗?我稍微更改了上面的实体
Meta
。在这里,我更新了我添加的元:
。其中(“userId=:userId”,{userId:id})
,但它不会在元表中添加任何更改。为什么?您应该使用
user
not
userId
一个问题,可能与此问题无关。我有用户实体和用户数据实体。用户具有作为UserDataEntity的关系。通过使用
oneToMany
,在UserDataEntity中,si创建了一个类似
userId
的列。是否可以根据userId从UserDataEntity访问行?因为即使创建了userId列,但我得到一个错误,即未找到
userId
列。您能帮忙吗?要从UserDataEntity访问用户,您应该为用户ID定义关系装饰器