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

this undefined in Reactive data #77

Open
harry-73 opened this issue Sep 21, 2022 · 3 comments
Open

this undefined in Reactive data #77

harry-73 opened this issue Sep 21, 2022 · 3 comments

Comments

@harry-73
Copy link

I use vue3 and vue-meteor-tracker 3.0.0beta7.

When I try to use 'this' in Reactive data, i got undefined.

In fact, it works when I use for example 'this.$store...' but not if I use 'this' with variables defined into 'data()' or 'computed()'.

@harry-73
Copy link
Author

harry-73 commented Sep 26, 2022

if the variable is coming from a prop it is OK:

export default {
props: {
id: { type: [String], required: true },
},

data() {
return { fromData: 'foo' };
},

meteor: {
test() {
console.log('Prop: ', this.id); // Result: OK
console.log("FromData", this.fromData); // Result: undefined
// code
}
}
}

@harry-73
Copy link
Author

harry-73 commented Oct 27, 2022

all reactive data, computed properties are undefined the fisrt time test() is called. The next time, it is OK.

It seems VueMeteor is added before created() of the component so all reactive data, computed properties are not yet been set up.

@jamesgibson14
Copy link

jamesgibson14 commented Sep 14, 2023

That is correct, vueMeteor uses beforeCreate https://github.com/meteor-vue/vue-meteor-tracker/blob/441a6ad2b125862a804e1a7b394a521749a14990/src/index.ts#L204C24-L204C24
I just ran into this problem and in Vue3 beforeCreate is run before the data() / computed() functions which means reactive dependencies are not setup. You can bypass it by creating your reactive variable in the setup(). Or a fix might be to change vueMeteor to use the "created" hook instead of "beforeCreate".

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

No branches or pull requests

2 participants