简体   繁体   中英

Run transaction regardless if collection/document exists - Firestore - Cloud Functions

I want to reward a user when he undertakes an action. It can happen the path to his 'coins' does not exists yet. That is why I get the error:

Transaction failure: Error: The data for XXX does not exist.

How can I run a transaction while the path can not exist yet? This is what I tried:

exports.facebookShared = functions.firestore.document('facebookShared/{randomUID}').onCreate(event => {
    const data = event.data.data()
    const uid = data.uid
    var promises = []
    promises.push(
        db.collection('facebookShared').doc(event.data.id).delete()
    )
    const pathToCoins = db.collection('users').doc(uid).collection('server').doc('server')
    promises.push(
       db.runTransaction(t => {
            return t.get(pathToCoins)
                .then(doc => {
                    var newCoins = 0
                    if (doc.data().hasOwnProperty("coins")){
                        newCoins = doc.data().coins + awardFacebookShare.coins
                    }
                        t.update(pathToCoins, { coins: newCoins });
                });
        })
        .then(result => {
            console.log('Transaction success', result);
        })
        .catch(err => {
            console.log('Transaction failure:', err);
        })
    )
    return Promise.all(promises)
})

I came across this docs: https://cloud.google.com/nodejs/docs/reference/firestore/0.8.x/Firestore#runTransaction

That docs are better than here: https://firebase.google.com/docs/firestore/manage-data/transactions

Below code works:

exports.facebookShared = functions.firestore.document('facebookShared/{randomUID}').onCreate(event => {
    const data = event.data.data()
    const uid = data.uid
    var promises = []
    promises.push(
        db.collection('facebookShared').doc(event.data.id).delete()
    )
    const pathToCoins = db.collection('users').doc(uid).collection('server').doc('server')
    promises.push(
       db.runTransaction(t => {
            return t.get(pathToCoins)
                .then(doc => {
                    var newCoins = awardFacebookShare.coins
                    if (doc.exists){
                    if (doc.data().hasOwnProperty("coins")){
                        newCoins += doc.data().coins
                    }
                        t.update(pathToCoins, { coins: newCoins });
                        return Promise.resolve(newCoins);
                }else{
                    t.create(pathToCoins, { coins: newCoins });
                    return Promise.resolve(newCoins);
                }
                });
        })
        .then(result => {
            console.log('Transaction success', result);
        })
        .catch(err => {
            console.log('Transaction failure:', err);
        })
    )
    return Promise.all(promises)
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM