简体   繁体   中英

How to make sure that the onUpdate method of the firebase database only runs after the onCreate method completes

I am trying to sync Firebase database data with a Google spreadsheet. For this, I have two methods that run onCreate and onUpdate in the database in a specific path. The problem is that sometimes the method called by the onUpdate callback is executed before the method called by the onCreate callback. How can I make sure the onCreate method is complete before doing something in onUpdate Method ? By the way, here is the code. Maybe I got some advice about the code in general, because now is the most difficult time to work with the Firebase and JavaScript functions.

exports.appendrecordtospreadsheet = functions.database.ref(`${CONFIG_DATA_PATH}/{USERID}/{GAMENUMBER}`).onCreate(
        (snap) => {        
            var firstRecord = [];
            console.log(snap.ref.parent.key);
            const key = snap.ref.parent.key;
            firstRecord.unshift(key);
            firstRecord.push(snap.val()[0])
            return appendPromise({
                spreadsheetId: CONFIG_SHEET_ID,
                range: 'A:Z',
                valueInputOption: 'USER_ENTERED',
                insertDataOption: 'INSERT_ROWS',
                resource: {
                    values: [firstRecord],
                },
            });
        });

function appendPromise(requestWithoutAuth)
{
    return new Promise(async (resolve, reject) => 
    {
        const client = await getAuthorizedClient();
        const sheets = google.sheets('v4');
        const request = requestWithoutAuth;
        request.auth = client;
        return sheets.spreadsheets.values.append(request, (err, response) => {
            if (err) {
                console.log(`The API returned an error: ${err}`);
                return reject(err);
            }
            return resolve(response.data);
        });
    });
}

exports.UpdateRecordInSpreadSheet = functions.database.ref(`${CONFIG_DATA_PATH}/{USERID}/{GAMENUMBER}`).onUpdate(
        (change) => {            
            const key = change.after.ref.parent.key;
            const updatedRecord = change.after.val();
            updatedRecord.unshift(key); 
                        return updatePromise({
                            spreadsheetId: CONFIG_SHEET_ID,
                            range: 'A:X',
                            valueInputOption: 'USER_ENTERED',
                            resource: {
                                values: [updatedRecord],
                            },
                        });

        });

function updatePromise(requestWithoutAuth) 
{
    return new Promise(async (resolve, reject) => 
    {
        const client = await getAuthorizedClient();
        const sheets = google.sheets('v4');
        const updateRequest = requestWithoutAuth;
        updateRequest.auth = client;
        const getRequest = {
            spreadsheetId: CONFIG_SHEET_ID,
            range: 'A:A'
        };
        getRequest.auth = client;
        return sheets.spreadsheets.values.get(getRequest, (err, response) => {
            if (err) {
                console.log(`The API returned an error: ${err}`);
                return reject(err);
            }
            else {
                const index = response.data.values[0].lastIndexOf(updateRequest.resource.values[0][0]) + 1;
                updateRequest.range = `A${index}:Z${index}`;
                return sheets.spreadsheets.values.update(updateRequest, (err, response) => {
                    if (err) {
                        console.log(`The API returned an error: ${err}`);
                        return reject(err);
                    }
                    return resolve(response.data);
                });
            }
        });
    });
}

Cloud Functions doesn't guarantee order of events. In fact, two events might be processed at the same time. You will need to account for that in your code. Typically it requires use of a transaction to make sure that two concurrently running bits of code do not collide with each other.

There are no workarounds to this - it is just the way Cloud Functions works, and it ensures the product is able to scale up demand without losing events.

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