IanMinit
IanMinit
  • 发布:2021-10-20 17:37
  • 更新:2021-10-21 04:39
  • 阅读:382

clientDB API 和 云函数如何区分

分类:uniCloud
const db = uniCloud.database()  
db.collection('list').where({name: "hello-uni-app"}).get()

上面这段代码,应该是在云函数和clientDB api里都可以运行的。
是否这段代码放在 unicloud/cloudfunctions 文件下就是云函数,放在客户端代码部分(比如 根目录/clientDB 文件夹下)就是clientDB?

2021-10-20 17:37 负责人:无 分享
已邀请:
hws007

hws007 - 我就是我!

云涵数:
放在 unicloud/cloudfunctions 文件下就是云函数
'use strict';
const db = uniCloud.database()
exports.main = async (event, context) => {
const dbCmd = db.command
const $ = dbCmd.aggregate
let res = await db.collection('unicloud-test').where(dbCmd.expr(
$.gte(['$time',$.dateFromString({
dateString: new Date('2020-02-02').toISOString()
})])
)).get()
return res
};

clientDB:放在客户端任意页或引用js上直接调用的
// 获取db引用
const db = uniCloud.database() //代码块为cdb
// 使用uni-clientDB
db.collection('list')
.where({
name: "hello-uni-app" //传统MongoDB写法,不是jql写法。实际开发中推荐使用jql写法
}).get()
.then((res)=>{
// res 为数据库查询结果
}).catch((err)=>{
console.log(err.code); // 打印错误码
console.log(err.message); // 打印错误内容
})

  • IanMinit (作者)

    原来放在外部JS里也算clientDB,感谢感谢

    2021-10-21 17:24

该问题目前已经被锁定, 无法添加新回复