dimanche 23 février 2020

how can i compare results using handlebars?

Use an example where I have a comparison block in Handlebars, it worked correctly, but some kind of update caused an error to begin, which after much research I can't solve.

My page shows a list of scheduled medical appointments for today, but there are scheduled appointments, it returns a message, otherwise a list of patients.

my listing.hbs




<p>Listado de  pacientes con consultas programadas para hoy</p>
<div>
    <table class="table table-striped">
        <thead>
        <tr>
            <th scope="col">Cédula</th>
            <th scope="col">Nombre</th>
            <th scope="col">Paciente desde</th>
            <th scope="col">Hora</th>
            <th scope="col" class="text-center">Acción</th>
        </tr>
        </thead>
        <tbody>
        <tr>
            
                    <td> </td>
                    <td> </td>
                    <td> </td>
                    <th scope="row"></th>
                    <td class="text-center">
                <a href="/pacientes/historia/" class="btn btn-secondary"><i class="fas fa-book-medical fa-2x"></i></a>

            </td>
                </tr>
            

        </tbody>
    </table>
</div>

my controller.js

 router.get('/profile', isLoggedIn, async (req,res)=>{
    const consultas = await pool.query('(....consulta....)');
    const totalConsultas = consultas.length;

    const inicio = {
        cero: 0,
        totalConsultas,
        consultas
    }
    console.log(inicio);
    res.render('profile',inicio);
});

my handlebars.js

const Handlebars = require("handlebars");
Handlebars.registerHelper('compare', function(lvalue, rvalue, options) {

    if (arguments.length < 3)
        throw new Error("Handlerbars Helper 'compare' needs 2 parameters");

    var operator = options.hash.operator || "==";

    var operators = {
        '==':       function(l,r) { return l == r; },
        '===':      function(l,r) { return l === r; },
        '!=':       function(l,r) { return l != r; },
        '<':        function(l,r) { return l < r; },
        '>':        function(l,r) { return l > r; },
        '<=':       function(l,r) { return l <= r; },
        '>=':       function(l,r) { return l >= r; },
        'typeof':   function(l,r) { return typeof l == r; }
    }

    if (!operators[operator])
        throw new Error("Handlerbars Helper 'compare' doesn't know the operator "+operator);

    var result = operators[operator](lvalue,rvalue);

    if( result ) {
        //console.log(lvalue, rvalue, operator, result)
        return options.fn(this);
    } else {
        //console.log(lvalue, rvalue, operator, result)
        return options.inverse(this);
    }

});

and my application index.js has this setting

const express = require('express');
// const bodyParser = require('body-parser');
const morgan = require('morgan');
const exphbs = require('express-handlebars');
const path = require('path');   
//settings
    app.set('port', process.env.PORT || 4000);
    app.set('views',path.join(__dirname,'views'));
    app.engine('.hbs', exphbs({
        defaultLayout:'main',
        layoutsDir: path.join(app.get('views'),'layouts'),
        partialsDir:path.join(app.get('views'),'partials'),
        extname:'.hbs',
        helpers: require('./lib/handlebars')
    }));
    app.set('view engine','hbs');

when I access the page the following error returns and I have not been able to find the solution

Error: Missing helper: "compare" at Object. (C:\Users\Rodolfo\Desktop\navarro\node_modules\express-handlebars\node_modules\handlebars\dist\cjs\handlebars\helpers\helper-missing.js:19:13) at Object.wrapper (C:\Users\Rodolfo\Desktop\navarro\node_modules\express-handlebars\node_modules\handlebars\dist\cjs\handlebars\internal\wrapHelper.js:15:19) at Object.eval [as main] (eval at createFunctionContext (C:\Users\Rodolfo\Desktop\navarro\node_modules\express-handlebars\node_modules\handlebars\dist\cjs\handlebars\compiler\javascript-compiler.js:262:23), :11:108) at main (C:\Users\Rodolfo\Desktop\navarro\node_modules\express-handlebars\node_modules\handlebars\dist\cjs\handlebars\runtime.js:208:32) at ret (C:\Users\Rodolfo\Desktop\navarro\node_modules\express-handlebars\node_modules\handlebars\dist\cjs\handlebars\runtime.js:212:12) at ret (C:\Users\Rodolfo\Desktop\navarro\node_modules\express-handlebars\node_modules\handlebars\dist\cjs\handlebars\compiler\compiler.js:519:21) at ExpressHandlebars._renderTemplate (C:\Users\Rodolfo\Desktop\navarro\node_modules\express-handlebars\lib\express-handlebars.js:250:12) at ExpressHandlebars. (C:\Users\Rodolfo\Desktop\navarro\node_modules\express-handlebars\lib\express-handlebars.js:173:21)

Aucun commentaire:

Enregistrer un commentaire