Skip to content

Instantly share code, notes, and snippets.

@zhgchgli0718
Last active March 23, 2021 16:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zhgchgli0718/09b7b6f12a81cb8361e6df23ab726f86 to your computer and use it in GitHub Desktop.
Save zhgchgli0718/09b7b6f12a81cb8361e6df23ab726f86 to your computer and use it in GitHub Desktop.
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