[go: nahoru, domu]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I can't retrieve the previous document. const previousData = change.before.data(); #1470

Closed
Patrick386 opened this issue Oct 16, 2023 · 3 comments
Assignees

Comments

@Patrick386
Copy link
Patrick386 commented Oct 16, 2023

const previousData = change.before.data(); I can't retrieve the previous document.
The 'previousData' log value is undefined.
How can I get document data?

"firebase": "^8.6.8",
"firebase-admin": "^11.10.1",
"firebase-functions": "^4.4.1",

Node.js v18.13.0

exports.
    .firestore.document("chatMessages/{messageId}")
    .onWrite(async (change, context) => {
        const newData = change.after.exists ? change.after.data() : null;
        // const previousData = change.before.exists ? change.before.data() : null;
        const previousData = change.before.data();

        console.log(">>> previous chat data status: ", previousData);
        console.log(">>> new chat data status: ", newData?.status);
    });
스크린샷 2023-10-16 오전 10 55 56 스크린샷 2023-10-16 오전 11 05 39
@google-oss-bot
Copy link
Collaborator

I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.

@blidd-google
Copy link
Contributor
blidd-google commented Oct 16, 2023

The onWrite trigger is invoked for all changes to a document; if you are creating a new document, then there will be no data in the before property.

However I would first recommend that you try out 2nd gen Firestore triggers! You can find the docs here; the 2nd gen triggers have a slightly different interface, but the logic is similar.

I'm going to close this since GitHub issues are reserved for reporting bugs, but you can reach out to Firebase Support here if you continue to have trouble writing Firestore functions.

@Patrick386
Copy link
Author

The onWrite trigger is invoked for all changes to a document; if you are creating a new document, then there will be no data in the before property.

However I would first recommend that you try out 2nd gen Firestore triggers! You can find the docs here; the 2nd gen triggers have a slightly different interface, but the logic is similar.

I'm going to close this since GitHub issues are reserved for reporting bugs, but you can reach out to Firebase Support here if you continue to have trouble writing Firestore functions.

understand. Let's use the 2nd trigger. thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants