How to sync with DB [SOLVED]

Hi,

So I was hoping for my app to wait untill the email is verified. So I used this:

    setInterval(async () => {
      const user = await Moralis.User.current();
      console.log(user.emailVerified);
    }, 5000)

However the user always returns the same object, no matter how I change the DB. So is await Moralis.User.current() a proper way to handle this kind of things? Or maybe I should use cloud functions for that?

what did you change in db for that user?

I would expect to see the updated object, you can also use a cloud function if you want

Well I’m changing emailVerifed field. Changing to true/false but I always get the same result. So when I change the DB properties the Moralis.User.current() always returns the same object. Unless I relog, then It’ll change. It looks like this method is calling some “cached” response object in the browser, not the server and the DB?

So is it possible to get synced data from DB without cloud functions? Or cloud functions are the proper way?

Ok, managed to find a solution. Looks a bit overengineered, but it works:

<script>
import {defineComponent} from "vue";
import Moralis from "moralis";
import {mapGetters} from "vuex";

export default defineComponent ({
  computed: {
    ...mapGetters('user', ['user']),
  },
  mounted() {
    setInterval(async () => {
      const emailVerified = await this.emailVerifiedLookup();
      console.log('Email Verified: ', emailVerified);
    }, 5000)
  },
  methods: {
    async emailVerifiedLookup() {
      const user = await Moralis.User.current();
      const query = new Moralis.Query(user);
      query.equalTo("objectId", this.user.id);
      const response = await query.first();
      return response.get('emailVerified');
    }
  }
})
</script>

I’m using Vue.js and vuex store to get the current user from the state of the app. However the solution is within async emailVerifiedLookup() function.

But besically what is going on is you have to query for the current user. As just calling user.get('emailVerified') doesn’t perform what we need. It think it just calles an object inside the session - that’s why it doesn’t update fields from the DB.

Once you have the current user as a responce you can get any field you want!

1 Like