云函数创建的记录通过小程序端无法更新?
发布于 7 年前 作者 oli 14222 次浏览 来自 官方Issues

目前碰到云函数创建的记录无法通过小程序端更新的问题:

同一用户(openid 一致)先通过云函数创建了一条新纪录,然后在小程序端去修改该记录,这个时候会报错

WAService.js:1 Uncaught (in promise) Error: errCode: -1  | errMsg: document.get:fail Error: cannot find document with _id , please make sure that the document exists and you have the corresponding access permission; at document.get api;

这条记录在数据库中是存在的。

云函数创建记录的代码

exports.main = async (event, context) => {
    const openid = cloud.getWXContext().OPENID;
 
    const { total } = await collection.where({ openid }).count();
 
    if (Object.is(total, 0)) {
        await collection.add({ data: { _openid: openid, openid, name: '默认名称', id_deleted: false, created_at: Date.now() } });
    }
 
    return await collection.get();
}

小程序端更新记录的代码

save(event) {
    const { name, _id, openid } = this.data;
 
    if (isEmpty(name)) return;
 
    const collection = wx.cloud.database().collection(COLLECTIONS.ACCOUNT_BOOK);
 
    collection.doc(_id).get({
        data: { name }
    }).then(res => {
        wx.showToast({ title: '保存成功', icon: 'none', mask: true });
    })
}

云函数创建的记录是否只能通过云函数更新?为什么不能通过小程序端更新?

3 回复

云函数创建的是管理员权限创建,也是不包含强制添加的_openid字段的。小程序端好像只能改当前openid创建的记录。改还是用云函数吧

是的,小程序端只能修改小程序端创建的记录

你可以在云函数创建的DOC里带上_openid,这样该openid就能update了,如果没有_openid,则不能。

回到顶部