0

I am attempting to get my app to render my data in an array from Firestore in SSR using asyncData. My function outside of asyncData is working fine but I can NOT get it to work inside.

I have tried so many ways of getting the array from asyncData to render. I trying to return the data onto 'addons' so it fills out the form the same way my method does.

Here is my working method

async getAddons() {
      var addonsRef = db
        .collection('addons')
        .where('publish', '==', true)
        .orderBy('timeUpdated', 'desc');
      await addonsRef.get().then(snapshot => {
        snapshot.forEach(doc => {
          const addons = {
            ...doc.data(),
            id: doc.id
          };
          return this.addons.push(addons);
        });
      });
    },

And here is the code I am currently trying inside asyncData

async asyncData({ app, error }) {
    const addonsRef = await db
      .collection('addons')
      .where('publish', '==', true)
      .orderBy('timeUpdated', 'desc');
    try {
      await addonsRef.get().then(snapshot => {
        snapshot.forEach(doc => {
          const addons = {
            ...doc.data(),
            id: doc.id
          };
          return app.addons.push(addons);
        });
      });
    } catch (e) {
      // TODO: error handling
      console.error(e);
    }
    return app.addons.push(addons);
  },

I expect the asyncData code to populate the array so it fills out my template the same as the getAddons function.

What simple thing am I missing?

5
  • What result of app.addons.push(addons) and how u reference it in tempalte Commented May 8, 2019 at 19:37
  • I am getting an empty array according to a console.log The template is using {{addon.name}} and such. Commented May 8, 2019 at 19:44
  • so thats the problem. u are getting empty array. asyncDAta will be executed on server on initial page load Commented May 8, 2019 at 20:08
  • That's not a problem, that is what is expected. But is should be returning data that will prerender. I already have it working on view pages that do a single call and not ask for a collection. Commented May 8, 2019 at 20:20
  • Turns out the problem was user error. I was trying to use asyncData inside a component and not on a page. arg. Thanks for the help @aldarund Commented May 9, 2019 at 2:39

1 Answer 1

1

you should return dictionary from asyncData. E.g.

{
 something:  app.addons.push(addons)
}

And then in template you access it via {{something. }}

Sign up to request clarification or add additional context in comments.

1 Comment

Tried that and I am still not getting anything back.

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.