Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

428
Vistas
how to to check inputs to JSON.parse() is valid or not in ejs

i'm working on document verification project.

when the document issued by the user exists,the program returns correct JSON result.

here is the output when i run

<% console.log('fit', JSON.parse(documents, null, '\t')) %>

and when the document exists(expected output),

Output fit [ { Key: 'DOCUMENT0', Record: { name: 'bachelor of science degree', url: 'https://bitcoin.org/ggg.pdf', issuedBy: 'sol123', dateOfIssuance: '12:18 PM, 25 September, 2021', hashedDoc: 'dac729a8acf4b8a88f73f5bd84206c34e01e0992efa251b772f68696e2c2539c9ed0090e73ef6b87dc24e3177c6fd5341c3e9e24ef14267ce07ab9428aeed897', docType: 'Whitepaper' } } ]

but,when the document is not issued by the user,for example,if the user is new and logged for the first time, it returns the following error.

SyntaxError: /home/verification/fabcar/javascript/views/pages/dashboard.ejs:15 13| 14|

15| <% JSON.parse(documents).forEach(document => { %> 16| <a class="list-group-item list-group-item-action" id="<%= document.Key %>" data-bs-toggle="tab" 17| href="#list-<%= document.Key %>" role="tab" aria-controls="list-<%= document.Key %>" style="min-width: 200px;"><%= document.Key %> 18| <% }) %>

Unexpected end of JSON input at JSON.parse (

i tried to check the content of documents when there is no document issued by the user or when a user logged to the system for the first time, content of json object when there is no document issued by the user,

{ match: true, _locals: [Object: null prototype] {}, username: 'uuu123', password: '09283ba5752cf589bad68e1d60d8165183386b1c75ba01c1d662719bbb571580bc7647667975c9d0410ae48d5e2e97bc3af206e7c21a2bc5624309bb237bdddd', documents: Buffer }

my question is,how can i display "No document issued" message on EJS when there is no document issued by the user? how can i use value set for documents when there is no document issued by the user in if else statements in ejs? please i need your help

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

It seems like the parse should be happening in the controller, where you can try-catch it and handle the case appropriately. In general, views should contain very little "business logic" and instead should just display data already handed to them.

For example, do the parse in a controller and if it succeeds, pass the parsed documents to the view. If the parse fails, render the view without the documents.

Then, the view can just check if documents is defined. If not, show the "No document" message.

Pseudocode example:

// In the controller
class MyController {
    getDocumentsView() {
        const viewData = {}

        try {
            viewData.documents = JSON.parse(documents)
        } catch (e) {
            console.log('Error parsing documents:', documents)
        }

        return view('myViewName.ejs', viewData)
    }
}
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda