I have a function to adding player like so:
inside my external js (player.js)
module.exports.player = function(appPlayer, db){
appPlayer.post('/', async(req, res) => {
const data = req.body
// data
const ipAdress = data.ipAdress
const carslidekey = data.id
const nickname = data.nickname
// random color for
const randomCol = Math.floor(Math.random()*16777215).toString(16)
let player = {
ipAdress: ipAdress,
nickname: nickname,
color: `#${randomCol}`
}
await db.collection('kartesian').doc(carslidekey)
.collection('player')
.doc(ipAdress)
.set(player).then(() => {
let dataResponse = {
status: true,
idDoc: ipAdress,
ipAdress: ipAdress,
color: `#${randomCol}`
}
const jsonStr_1 = JSON.stringify(dataResponse)
res.send(jsonStr_1)
}).catch((err) => {
let dataResponse = {
status: false,
idDoc: ipAdress,
ipAdress: ipAdress,
msg: `Terjadi kesalahan ${err.data}`
}
const jsonStr_1 = JSON.stringify(dataResponse)
res.send(jsonStr_1)
})
})
}
and my index.js (cloud functions) i wrote https request like below:
......
const playerHandler = require('./src/player')
playerHandler.player(app, db)
exports.player = functions.https.onRequest(app)
......
My problem is sometime this function called by different devices and might be concurrently, even though I've created a method set
with a different id
but why sometimes I find the previous documentation replaced by the document after. how to ensure that there is no overlap? Thanks