Last active
March 23, 2021 16:15
-
-
Save zhgchgli0718/09b7b6f12a81cb8361e6df23ab726f86 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const functions = require('firebase-functions'); | |
const admin = require('firebase-admin'); | |
const express = require('express'); | |
const cors = require('cors'); | |
const app = express(); | |
admin.initializeApp(); | |
app.use(cors({ origin: true })); | |
// Distributed counters Like Post | |
app.post("/like2/:id", async (req, res) => { | |
const doc = await admin.firestore().collection('posts').doc(req.params.id).get(); | |
const increment = admin.firestore.FieldValue.increment(1) | |
if (!doc.exists) { | |
return res.status(404).send({"message":"找不到文章!"}); | |
} | |
//1~10 | |
await admin.firestore().collection('posts').doc(req.params.id).collection("likeCounter").doc("likeCount_"+(Math.floor(Math.random()*10)+1).toString()) | |
.set({count: increment}, {merge: true}); | |
res.status(201).send({"message":"按讚成功!"}); | |
}); | |
exports.post= functions.https.onRequest(app); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment