Google 클라우드 기능을 사용하여 사용자가 실시간 데이터베이스와 상호 작용할 API 끝점을 만듭니다.Google 클라우드 기능에서 실시간 데이터베이스의 원자 업데이트
내가 가진 문제는 코드가 어떻게 작동하는지 모르겠다는 것입니다. 나는 한 번만 호출해야하는 도우미 함수 doSomething
을 가지고 있지만 여러 번 호출 할 수있는 경우가있을 수 있습니다 (여러 사용자가 동시에 API를 호출 할 때 업데이트 작업이없는 경우가 있음). 아직 DB에 의해 처리되었습니다). 가능한가? 방법이 transaction
일 필요가 있다는 것을 의미합니까? 고맙습니다!
DB 구조 나는 당신이 완전한 감각을하지 인해 위의 의사에을 downvoted 받았다고 생각
{
somePath: {
someSubPath: null
}
}
Google 클라우드 기능 코드
const functions = require('firebase-functions')
const admin = require('firebase-admin')
const cors = require('cors')({origin: true});
admin.initializeApp(functions.config().firebase)
// API ENDPOINT
exports.test = functions.https.onRequest((req, res) => {
cors(req, res,() => {
admin.database().ref('/somePath/someSubPath').once('value')
.then(snapshot => {
const value = snapshot.val()
if (value) return res.status(400).send({ message: 'doSomethingAlreadyCalled' })
doSomething()
const updates = { '/somePath/someSubPath': true }
return admin.database().ref().update(updates)
.then(() => res.status(200).send({ message: 'OK' }))
})
.catch(error => res.status(400).send({ message: 'updateError' }))
})
})
// HELPERS
const doSomething =() => {
// needs to be called only once
}
는 흠 ... 나는 궁금 downvote 모두에 대한 –