如何在 hbs 模板中显示数据?

How to show data in a hbs template?

需要一些帮助,我正在尝试在模板中显示数据,但它不起作用。我在 mysql.

中使用 NestJS

这里是controller.ts的代码:

import { Controller, Get, Post, Put, Delete, Body, Param, Render, UsePipes, Logger, UseGuards} from '@nestjs/common';
import { ProductoService } from './producto.service';
import { ProductoDTO } from './producto.dto';
import { ValidationPipe } from '../shared/validation.pipe';


@Controller('producto')
export class ProductoController {
    private logger = new Logger('ProductoController');

    constructor(private productoService: ProductoService){}

    @Get('index')
    //@UseGuards(new AuthGuard())
    @Render('Producto/index')
    showAllUsers(){
        return {product:this.productoService.showAll().then(function(result){
            var aux = [{}];
            aux = result;
            console.log(aux);
            return aux;

        })};        
    }
}

这里是 index.hbs 车把模板的代码:

<h2>Lista de Productos</h2>
<div class="entry">
    <table class="table">
        <tr>
            <th>Nombre</th>
            <th>Cantidad</th>
            <th>Descripcion</th>
            <th></th>
        </tr>
        <tr>           
            {{#each aux}}
                <td>{{this.name}}</td>
                <td>{{this.quantity}}</td>
                <td>{{this.description}}</td>
            {{/each}} 
        </tr>
    </table>
    <br/>
</div>

甚至在控制台日志中显示数据。

我不认为 nestjs 会自动等待嵌套的承诺解决。所以它会 return {product: Promise}.

我建议采用你的方法 async:

@Get('index')
async showAllUsers(){
    const products = await this.productoService.showAll();
    return {products};        
}

此外,在您的 hbs 模板中,您指的是 aux,尽管您 return {product: [...]}。所以它应该是 {{#each product}} 。 (我在上面的示例中将其重命名为 products,因为它是一个数组。)