Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
394 views
in Technique[技术] by (71.8m points)

vue.js - Vue's composition API ref contains data returns null when .value is called

I have a composable that fetches and returns ref documents in real-time from Firestore. The ref contains data but when I call .value in the setup(), null is returned.

The composable is as follows.

import { watchEffect, ref } from "vue";
import { projectFirestore } from "../firebase/config";

const getDocument = (collection, id) => {
  let document = ref(null);
  let error = ref(null);

  let documentRef = projectFirestore.collection(collection).doc(id);

  const unsub = documentRef.onSnapshot(
    doc => {
      if (doc.data()) {
        document.value = { ...doc.data(), id: doc.id };
        error.value = null;
      } else {
        error.value = "That document does not exist.";
      }
    },
    err => {
      console.log(err.message);
      error.value = "Problem fetching the document.";
    }
  );

  watchEffect(onInvalidate => {
    onInvalidate(() => unsub());
  });
  return { error, document };
};

export default getDocument;

When I dump the result of this, console.log(result), I get the output in the capture below but result.value is null.

How can I get the values within setup()?

enter image description here

Please note that the capture is a result of console.log(result) & console.log(result.value).

question from:https://stackoverflow.com/questions/65644786/vues-composition-api-ref-contains-data-returns-null-when-value-is-called

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...