当前位置:网站首页>Like cloud functions
Like cloud functions
2022-07-01 04:53:00 【JSONP$】
// Cloud function entry file
const cloud = require('wx-server-sdk')
cloud.init({
env: cloud.DYNAMIC_CURRENT_ENV
})
const db = cloud.database()
const formatTime = date => {
const year = date.getFullYear()
const month = date.getMonth() + 1
const day = date.getDate()
const hour = date.getHours()
const minute = date.getMinutes()
const second = date.getSeconds()
return `${
[year, month, day].map(formatNumber).join('-')} ${
[hour, minute, second].map(formatNumber).join(':')}`
}
const formatNumber = n => {
n = n.toString()
return n[1] ? n : `0${
n}`
}
// Cloud function entry function
exports.main = async (event, context) => {
const wxContext = cloud.getWXContext()
let dz = db.collection('movie_zan_collect')
let {
total} = await new Promise( (resolve, reject)=>{
dz.where({
member_openid: wxContext.OPENID,movie_id: event.movieId }).count().then( res=>{
resolve(res)
})
})
let data = {
member_openid: wxContext.OPENID,
movie_id:event.movieId,
like:event.like,
collection:event.collection,
_updateTime:formatTime(new Date()),
}
if( total == 0 ){
console.log(data)
data._createTime = formatTime(new Date())
return await dz.add({
data }).then( res=>{
console.log(' Added successfully ->', res)
return res
}).catch( err=>{
console.log(' Add failed ',err)
})
}else{
return await dz.where({
member_openid: wxContext.OPENID,movie_id: event.movieId }).update({
data }).then( res=>{
console.log(' The update is successful ->', res)
return res
}).catch( err=>{
console.log(' Update failed ',err)
})
}
}
Multi research
Let's look at another interface
// Cloud function entry file
const cloud = require('wx-server-sdk')
cloud.init({
env: cloud.DYNAMIC_CURRENT_ENV
})
const db = cloud.database()
// Cloud function entry function
exports.main = async (event, context) => {
const wxContext = cloud.getWXContext()
return db.collection('movie_zan_collect').where({
movie_id:event.movieId,
member_openid:wxContext.OPENID
}).get()
}
Implement this logic , Two interfaces are required
边栏推荐
- Dede collection plug-in does not need to write rules
- [FTP] common FTP commands, updating continuously
- Sqlplus connects using the instance name
- 1076 Forwards on Weibo
- Detailed explanation of distributed global unique ID solution
- 对象的序列化与反序列化
- Matters behind the construction of paint testing laboratory
- Thread safety issues
- Leecode record 1351 negative numbers in statistical ordered matrix
- 分布式全局唯一ID解决方案详解
猜你喜欢
随机推荐
C - detailed explanation of operators and summary of use cases
Print stream and system setout();
STM32 photoresistor sensor & two channel AD acquisition
Query long transaction
[daily question in summer] letter delivery by p1629 postman in Luogu (to be continued...)
Leecode question brushing record 1310 subarray XOR query
LeetCode_ 58 (length of last word)
Common methods in transforms
缓冲流与转换流
数据加载及预处理
Introduction to JVM stack and heap
[FTP] common FTP commands, updating continuously
Buffer stream and transform stream
[summer daily question] Luogu p5886 Hello, 2020!
STM32 extended key scan
对象的序列化与反序列化
pytorch中常用数据集的使用方法
VIM easy to use tutorial
线程安全问题
Basic usage, principle and details of session









