3

I'm struggling with this for a few days, maybe you could help me. I'm trying to fetch data from firebase (saved in state.state.memory and display it on my template. My variable saved updates on console, but template doesn't shows anything. I tried everything but I can't get anything on the template, could you explain?

Thanks!!

template

<template>
  <div class="memory" id="memory">
    Memory
    <div class="elementos-memoria" v-for="item in saved" :key="item">
      {{ item.primero + item.operador + item.segundo + ' = ' + item.resultado }}
    </div>
    <div class="elementos-memoria" v-for="cuenta in cuentas" :key="cuenta">
      {{ cuenta }}
    </div>
  </div>
</template>

script

import M from "materialize-css";
import { inject, reactive, watch } from "vue";

export default {
  props: {
    cuentas: Array,
  },
  setup() {
    const state = inject("store");
    let saved = reactive({});
    watch(
      () => state.state.memory,
      () => {
        saved = state.state.memory;
        console.log(saved);
      }
    );

    setInterval(() => console.log(saved), 1000);

    return {
      saved,
    };
  },
  mounted() {
    M.AutoInit();
  },
};

Auth.js File

const state = reactive({
    user: {},
    memory: {},
})

const login = (email, password) => {
    let memory = []
    auth.signInWithEmailAndPassword(email, password).then((data) => {
        state.user = data.user
        db.collection('users').doc(state.user.uid).collection('operaciones').get().then((queryResult) => {
            queryResult.forEach(doc => {
                memory.push(doc.data())
            })
            state.memory = memory
        }).catch(err => console.log(err))
    })
}

const signIn = (email, password) => {
    auth.createUserWithEmailAndPassword(email, password).then(cred => {
        state.user = cred.user;
    })
}


const logout = () => {
    auth.signOut().then(() => {
        state.user = null;
        state.memory = null;
    })
}

1 Answer 1

7

Looks like you're replacing the reactive state value instead of mutating it

saved = state.state.memory; will replace it.

not sure what's in the state.state.memory object (assuming it is an object), but you can use Object.assign to dynamically update:

if (state.state.memory) {
  Object.assign(saved, state.state.memory)
}
Sign up to request clarification or add additional context in comments.

3 Comments

and why replacing it doesn't work? saved object should be identical to state.state.memory. I updated the code with state.state.memory information, thanks. I resolved it defining saved as array instead of an object but I would like to know what i'm doing wrong
because the object created during reactive({}); is not a regulat object but a Proxy , so when you re-assign it, the reactivity gets lost
You could even try using a ref, which you can re-assign that one by using .value however that won't watch for mutations within the object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.