小程序与云服务器api接口,小程序云函数调用http api进行对云数据库的操作

小程序http api 的云数据库操作接口需在服务器端调用,利用云开发后台的话,可以云函数中调用相应的接口可以实现对云数据库的操作。以下是新增集合的一个代码段,以供参考。

需要注意的是,安装got模块不能安装10.版本,否则或出错。

// 云函数入口文件

const cloud = require(‘wx-server-sdk’)

const got = require(‘got’)

let appid =‘xxxxxxx’ //你的appid

let secret =‘xxxxxxxx’ //你的secret

let opdataUrl =‘https://api.weixin.qq.com/tcb/databasecollectionadd?access_token=’ //新增集合的接口

let tokenUrl =‘https://api.weixin.qq.com/cgi-bin/token? grant_type=client_credential&appid=’+appid+’&secret=’+secret //获取access_token接口

cloud.init()//初始化

// 云函数入口函数

exports.main = async (event, context) => {

let tokenResponse=await got(tokenUrl)

let token = JSON.parse(tokenResponse.body).access_token //获取access_token

let opdataResult = await got(opdataUrl+token, {

method: ‘POST’,

headers: {

‘Content-Type’: ‘application/json’

},

body: JSON.stringify({

“env”:“xxxxx”, //云环境

“collection_name”: “test_add_collection” //新增集合名称

})

})

return opdataResult.body

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值