UNIAPP 云对象中使用数据库报“Error: 权限校验未通过,未能获取当前用户信息,当前用户为匿名身份,“错误

    getInitecode:async function(){
        const db=uniCloud.databaseForJQL()
        const id=await this.uniID.checkToken(this.getUniIdToken())
        const res=await db.collection('uni-id-users').where('_id=="'+id.uid+'"').field('my_invite_code').get()
        return res
    }

在保证数据库权限和用户权限没有问题的前题下,

如上代码,会报"Error: 权限校验未通过,未能获取当前用户信息,当前用户为匿名身份,"

问题是在代码第二行,

即使你在前段登录,在本地有TOKEN,但是在云函数里使用数据库连接时,并不能自动传送本地TOKEN信息到数据库连接.

这种情况只需要在第二行改为

const db=uniCloud.databaseForJQL({clientInfo:this.getClientInfo()})

然后就不报权限错误了.

倒数第二行JQL语法里where语句中,这样是正常工作的,但是文档中的,.where('_id==$cloudEnv_uid')写法不生效,会报个本地没有schema的错.

不知道什么原因.如下所示

//const res=await db.collection('uni-id-user').where('_id==$cloudEnv_uid').field('my_invite_code').get()

报错如下

有了解的同学请指导!~ 

3小时后==>

已解决,

表名少写了个s, 检查了n遍,也没有检查出来,浪费我半天时间.

下面是完整代码,留做笔记

//云对象中
const uniID = require('uni-id-common')
module.exports = {
	_before: function () { // 通用预处理器
		const clientInfo = this.getClientInfo()
		this.uniID = uniID.createInstance({ // 创建uni-id实例,其上方法同uniID
			clientInfo
		})
	},
	/**
	 * method1方法描述
	 * @param {string} a 传入本地token
	 * @returns {object} 返回uid
	 */
	checkLogin: async function(token){
		u= await this.uniID.checkToken(a)
		return u.uid
	},
	getInitecode:async function(){
		const db=uniCloud.databaseForJQL({clientInfo:this.getClientInfo()})
		const id=await this.uniID.checkToken(this.getUniIdToken())
//		db.setUser(id)
		//const res=await db.collection('uni-id-users').where('_id=="'+id.uid+'"').field('my_invite_code').get()
		  const res=await db.collection('uni-id-user').where('_id==$cloudEnv_uid').field('my_invite_code').get()
		return {errCode: 0,errMsg: res}
	}
}

云对象同级文件package.json,如果在新建云对象时选择了依赖,就不用管这个,主要是里面的dependencies

{
  "name": "touserget",
  "dependencies": {
    "uni-id-common": "file:../../../uni_modules/uni-id-common/uniCloud/cloudfunctions/common/uni-id-common",
    "uni-config-center": "file:../../../uni_modules/uni-config-center/uniCloud/cloudfunctions/common/uni-config-center"
  },
  "extensions": {
    "uni-cloud-jql": {}
  },
    "cloudfunction-config": {
  		"memorySize": 256,
  		"timeout": 5,
  		"triggers": [{
  				"name": "myTrigger",
  				"type": "timer",
  				"config": "0 0 2 1 * * *"
  		}],
  		"path": "",
  		"runtime": "Nodejs14"
  	}
}

前端

	async function go3() {
	
		const todo = uniCloud.importObject('sj-user')
		await todo.getInitecode().then((res)=>{
			console.log(res);
		})
	}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值