Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to read JSON in Pug NodeJs #3431

Open
FranciscoCastle opened this issue Apr 10, 2024 · 1 comment
Open

How to read JSON in Pug NodeJs #3431

FranciscoCastle opened this issue Apr 10, 2024 · 1 comment

Comments

@FranciscoCastle
Copy link

Pug Version: 3.0.2

Node Version: 21.7.1

Input JavaScript Values

let data = JSON.parse('{"memberships":{"view":0},"forms":{"view":1}}');
res.render('test',{data:data});

Input Pug

p= data.forms.view

Expected HTML

<p>1</p>

Actual HTML

<p>[object Object]</p>

Additional Comments

I have tried to access from pug a json that I sent in my response from the server to the web client, but I have not had success reading the json, is there a way to read it?

@justaprogrammer321
Copy link

Hey @FranciscoCastle I have running your code and the code runs perfectly fine

app.js

const express = require('express');
const app = express();

app.set('view engine', 'pug');
app.set('views', './views');

app.get('/', (req, res) => {
let data = JSON.parse('{"memberships":{"view":0},"forms":{"view":1}}');
console.log(data)
res.render('index',{data});
  });

app.listen(3001, () => console.log('Server running on port 3001'));

and Inside the views directory
Create an index.pug and enter the following code

index.pug

doctype html
html
  head
    title= title
  body
    p data: #{data.forms.view} 

Ouput I recieved was

data: 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants