Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

426
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!