O
O
Oleg Voitenko2021-08-02 18:54:12
Node.js
Oleg Voitenko, 2021-08-02 18:54:12

How to get a promise?

Hello, I'm using Expres+TypeORM+routing-controllers
The application renders pages from ejs.

I can't understand why I get an empty object from the controller.

Controller code:

@Controller()
@Get('/dashboard')
@Render ('dashboard')
    getDashboard () {
        const sites = this.usersSitesService.selectSitesByUserId(1);

        return { sites };
    }


Service code:
selectSitesByUserId =  async (id: number): Promise<any> => {
        const res = await this.sitesRepository.createQueryBuilder('s')
            .leftJoin('usites', 'u', 's.id = u.site_id')
            .where('u.user_id =:id', {id: id}).getMany();
        return res;
    }


code in ejs file
<ul class="nav nav-treeview">
                            <% for(let i = 0; i < sites.length; i++) { %>
                            <li class="nav-item">
                                <a href="/dashboard/sites/?siteId=<%= sites[i].id %>" class="nav-link">
                                    <p><%= sites[i].siteName %></p>
                                </a>
                            </li>
                            <% } %>
                        </ul>


But the site itself arrives empty - {}

What am I doing wrong?

Repo with docker router-controller https://github-com.translate.goog/typestack/routin...

return { sites }; - this is required by the dock with the @render decorator. Only in this syntax are variables passed to the renderer. And if you fill the sites with handles, then everything works.

I will be grateful for your help!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Belyaev, 2021-08-02
@OliverV

And who will wait for the asynchronous action?

@Controller()
@Get('/dashboard')
@Render ('dashboard')
    async getDashboard () {
        const sites = await this.usersSitesService.selectSitesByUserId(1);

        return { sites };
    }

selectSitesByUserId =  async (id: number): Promise<any>
What's the point of a typescript if you write any?
Repo with docker router-controller https://github-com.translate.goog/typestack/routin...
Link to machine translation? Are you serious?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question