9***@qq.com
9***@qq.com
  • 发布:2025-07-02 11:55
  • 更新:2025-07-02 14:06
  • 阅读:123

【报Bug】云对象URL化访问方法,checkToken可以通过查询数据表返回:TOKEN_INVALID_ANONYMOUS_USER:未能获取当前用户信息:当前用户为匿名身份。

分类:uniCloud

产品分类: uniCloud/App

操作步骤:

前端调用代码

export const addAddressRequest = function (data: Address): Promise<ResponseData> {  
        return request.post('/addAddress', {  
            method: 'POST',  
            data  
        })  
}  
const data = {  
        id: '68648c18213929ab1d9d6c47',  
        mobile: '17777777777',  
        username: '李小明',  
        region: ['河南省', '郑州市', '金水区'],  
        address: '大学路'  
    }  
    const result = await addAddressRequest(data)  
"""  
## 后段云对象代码  
“”“  
const uniID = require('uni-id-common')  
const {  
    addAddress,  
    getAddress  
} = require('./module/user')  
module.exports = {  
    _before: function() {  
        this.token = this.getHttpInfo().headers.token  
        const clientInfo = this.getClientInfo()  
        this.uniID = uniID.createInstance({ // 创建uni-id实例,其上方法同uniID  
            clientInfo  
        })  
    },  
    async addAddress() {  
        const data = JSON.parse(this.getHttpInfo().body)  
        const payload = this.uniID.checkToken(this.token)  
        const result = await uniCloud.databaseForJQL({  
            clientInfo: this.getClientInfo()  
        }).collection('yjs-address').add(data)  
        return result  
    }  
}

预期结果:
{  
    errCode: 0,  
    errMsg: '',  
    id: '新增数据的id'  
}

实际结果:
{  
    "success": false,  
    "error": {  
        "code": "FunctionBizError",  
        "message": "TOKEN_INVALID_ANONYMOUS_USER:未能获取当前用户信息:当前用户为匿名身份"  
    }  
}

bug描述:

表结构

{  
  "bsonType": "object",  
  "permission": {  
    "read": true,  
    "create": true,  
    "update": true,  
    "delete": true  
  }  
}
module.exports = {  
    _before: function() {  
        this.token = this.getHttpInfo().headers.token  
        const clientInfo = this.getClientInfo()  
        this.uniID = uniID.createInstance({  
            clientInfo  
        })  
    },  
async addAddress() {  
        const data = JSON.parse(this.getHttpInfo().body)  
        const payload = this.uniID.checkToken(this.token)  
        const result = await uniCloud.databaseForJQL({  
            clientInfo: this.getClientInfo()  
        }).collection('yjs-address').add(data)  
        return result  
}  

}

我控制台是看了payload没有问题。是有uid的。
我的云端schema和本地schema增删改查权限全都是true。
不明白为什么访问数据库的时候未能获取当前用户信息。
求大佬帮解答

2025-07-02 11:55 负责人:无 分享
已邀请:
DCloud_uniCloud_CRL

DCloud_uniCloud_CRL

  • 9***@qq.com (作者)

    已解决,感谢

    2025-07-02 14:32

DCloud_UNI_yuhe

DCloud_UNI_yuhe

你是不是 url 方式没有成功传过去 token 呢?

  • 9***@qq.com (作者)

    我不明白的就是token需要特定的名字或者传输方式才能被校验到吗?

    我将token传到了headers里,我在云对象中查看了headers中token是可以看到的。


    requestInterceptor: (options) => {  
    options.header = {
    ...options.header,
    'content-type': 'application/json',
    token: uni.getStorageSync('uni_id_token') || ''
    }
    return options
    }

    2025-07-02 12:06

  • DCloud_UNI_yuhe

    回复 9***@qq.com: 你打印一下 checktoken 之前的看一下传过去了没有,一般是没有取到吧

    2025-07-02 12:09

  • 9***@qq.com (作者)

    回复 DCloud_UNI_yuhe: 可以取到。


    async addAddress() {  
    console.log(this.token);
    const data = JSON.parse(this.getHttpInfo().body)
    const payload = this.uniID.checkToken(this.token)
    const result = await uniCloud.databaseForJQL({
    clientInfo: this.getClientInfo()
    }).collection('yjs-address').add(data)
    return result
    }
    [yjs-client/ac1cc3191751429631990-85542/49ms/DEBUG] eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOiI2ODYzZjg0MTIxMzkyOWFiMWQ5MGZhYmQiLCJyb2xlIjpbInVzZXIiXSwicGVybWlzc2lvbiI6W10sInVuaUlkVmVyc2lvbiI6IjEuMC4xOCIsImlhdCI6MTc1MTQxNjczMywiZXhwIjoxNzUyMDIxNTMzfQ.yDuFlgHzD0FFCqf0lbf3f1a6u6Fou5tE2SqMjKJgqE4

    2025-07-02 12:17

要回复问题请先登录注册