在uni-app的uniCloud.database()下使用set()报错:JQL禁止使用set方法

在uni-app的uniCloud.database()下使用set()报错:JQL禁止使用set方法

产品分类

uniCloud/支付宝小程序云

示例代码

// 更新物品  
export const updateItemAPI = async (id, data) => {  
  try {  
    await uniCloud.database().collection('items').doc(id).set(data)  
    return res('更新物品成功', 0)  
  } catch (err) {  
    console.error(err)  
    return res(err)  
  }  
}

操作步骤

// 更新物品  
export const updateItemAPI = async (id, data) => {  
  try {  
    await uniCloud.database().collection('items').doc(id).set(data)  
    return res('更新物品成功', 0)  
  } catch (err) {  
    console.error(err)  
    return res(err)  
  }  
}

预期结果

'更新物品成功', 0

实际结果

Error: JQL禁止使用set方法

bug描述

报错图

update()是正常的,但是我的需求不能用update()我要全部覆盖数据,我都没用databaseForJQL还提示不能用set(),这是不是bug,是的话请快点修复,真着急,如果不是bug请告诉我我哪里用错了

官方文档


更多关于在uni-app的uniCloud.database()下使用set()报错:JQL禁止使用set方法的实战教程也可以访问 https://www.itying.com/category-93-b0.html

4 回复

想要用 set 指令,那就不要用JQL声明db对象,应该在云函数中使用声明传统数据库操作db对象,即 const db = uniCloud.database() ,注意: 只能在云函数或云对象中使用传统数据库操作db对象

更多关于在uni-app的uniCloud.database()下使用set()报错:JQL禁止使用set方法的实战教程也可以访问 https://www.itying.com/category-93-b0.html


原来如此,客户端不能操作传统数据库db对象是吧,只能在云函数或云对象中使用传统数据库操作db对象

回复 y***@foxmail.com: 是的,客户端只能操作JQL的db对象, 客户端就算使用 const db = uniCloud.database() 声明的也是JQL的db对象

在uniCloud的JQL模式下确实不支持set()方法,这是设计上的限制。正确的做法是使用update()方法配合merge:false参数来实现完全覆盖更新:

export const updateItemAPI = async (id, data) => {  
  try {  
    await uniCloud.database().collection('items').doc(id).update({
      data,
      merge: false
    })  
    return res('更新物品成功', 0)  
  } catch (err) {  
    console.error(err)  
    return res(err)  
  }  
}
回到顶部