1

When querying Firestore the browser is returning [object Object],[object Object] instead of:

{
>    Description: 'This is a job offer',
>    Active: true,
>    Location: 'Warsaw',
>    Name: 'BA in Bank',
>    Deadline: Timestamp { _seconds: 1603317600, _nanoseconds: 0 }
>  }
>  {
>    Description: 'This is another job offer',
>    Location: 'Amesterdam',
>    Name: 'PM job',
>    Active: true,
>    Deadline: Timestamp { _seconds: 1607554800, _nanoseconds: 0 }
>  }

This is a route and I would like to pass the array so I can render it on EJS view dynamically.

router.get('/projects', async(req, res) => {
    
        var array = [];
        const snapshot = await firebase.firestore().collection('projects').get()
        snapshot.forEach(doc => {
            array.push(doc.data());
        })
    
        res.render('projects', { array })
    
    });

Is there a way to pass these object values on the route handler?

4
  • You might want to try stringifying it in your router: res.render('projects', { JSON.stringify(array) }) Commented Aug 25, 2020 at 23:53
  • @FrankvanPuffelen I don't think that will help me, because I won't be able to manipulate it in EJS - even though I get your point. Commented Aug 26, 2020 at 0:04
  • Hi @PauloBrás could you please check these both cases here and here and confirm if they help you? They show different approaches for the router that might help you, including a difference on the order of how you are retrieving the data, that might be the cause for your issue. Commented Aug 26, 2020 at 13:16
  • @gso_gabriel thanks for that - it didn't have the answer but it had hints that allowed me to reach the solution! :) Commented Aug 27, 2020 at 9:53

1 Answer 1

1

I manage to find the answer thanks to @gso_gabriel suggestion.

It its ok to send Object on your express middleware but then you need to use forEach() on your EJS view so that it loops through your object and extracts the elements.


router.get('/projects', async(req, res) => {


    const snapshot = await firebase.firestore().collection("projects").get();
    
    const docs = snapshot.docs.map(doc => doc.data())

    res.render('projects', { docs });

});

Then on your EJS you need to do docs.forEach()

 <% docs.forEach(doc => { %>
                            <%- doc.Name; %>
                                <% }); %>

And in EJS you can use it to add whatever html elements you wish to have on your front-end.

I hope this helps for future users.

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

Comments

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.