REFERENCE ERROR html and Node.js

Asked

Viewed 62 times

-2

var express = require('express');
var router = express.Router();
var pessoas = [];

/* GET home page. */
router.get('/', function(request, response, next) {
  response.render('index', { title: 'Node.js com Express' });
});

router.post('/cadastrar-pessoa', function (request, response, next) {
    
    hash = {
           nome: request.body.nome,
      sobrenome: request.body.sobrenome,
            cpf: request.body.cpf,
       telefone: request.body.telefone,     
       endereco: request.body.endereco,

       
    };
    pessoas.push(hash);
      


    response.render('index', { title: 'cadastrar-pessoa', pessoas: pessoas });

});


module.exports = router;

<!DOCTYPE html>
<html>
  <head>
    <title><%= title %></title>
    <link rel='stylesheet' href='/stylesheets/style.css' />
    <script type="text/javascript" src="/javascripts/jquery.js"></script>
    <script type="text/javascript" src="/javascripts/validacao.js"></script>
  </head>
  <body>
    <h1><%= title %></h1>
    <p>Welcome to <%= title %></p>
    h1>Cadastro em tempo de execução</h1>

		<fieldset id="formCadastro">
			<legend>Cadastro de pessoas</legend>
			<form action="cadastrar-pessoa" method="POST" onsubmit=" return validacao()">
				<div>
					<label for="nome">Nome: </label>
					<input type="text" name="nome" id="nome" /> 
				</div>
				<div>
					<label for="sobrenome">Sobrenome:  </label>
					<input type="text" name="sobrenome" id="sobrenome" /> 
				</div>
				<div>
					<label for="cpf">CPF:  </label>
					<input type="text" name="cpf" id="cpf" /> 
				</div>
				<div>
					<label for="telefone">Telefone:  </label>
					<input type="text" name="telefone" id="telefone" /> 
				</div>
				<div>
					<label for="endereco">Endereço:  </label>
					<input type="text" name="endereco" id="endereco" /> 
				</div>
        <hr>
        <div>
					<input type="submit" value="Cadastrar" /> 
				</div>
			</form>
		</fieldset>
		
		<fieldset>
			<legend>Pessoas cadastradas</legend>
			<table border="1" id="registros">
				<tr>
					<td>Nome</td>
					<td>Sobrenome</td>
					<td>CPF</td>
					<td>Telefone</td>
					<td>Endereço</td>
				</tr>
				<% for (i=0; i<pessoas.length; i++) { %>
					<tr>
						<td><%= pessoas[i].nome %></td>
						<td><%= pessoas[i].sobrenome %></td>
						<td><%= pessoas[i].cpf%></td>
						<td><%= pessoas[i].telefone%></td>
						<td><%= pessoas[i].endereco%></td>
					</tr>
				<% } %>
			
			
			
			</table>
		</fieldset>

  </body>
</html>

the MISTAKE:

C: Users Desktop User Node backend views index.ejs:54 52| Address 53| >> 54| <% for (i=0; i < people.length; i++) { %> 55| 56| <%= people[i]. name %> 57| <%= persons[i]. surname %> persons is not defined Referenceerror: C: Users Desktop User Node backend views index.ejs:54 52| Address 53|

54| <% for (i=0; i < people.length; i++) { %> 55| 56| <%= persons[i]. name %> 57| <%= persons[i]. surname %>

people is not defined at Eval (Eval at Compile (C: Users Desktop User Node backend node_modules ejs lib ejs.js:633:12), :20:22) at returnedFn (C: Users Desktop User Node backend node_modules ejs lib ejs.js:668:17) at tryHandleCache (C: Users Desktop User Node backend node_modules ejs lib ejs.js:254:36) At view.exports.renderFile [as engine] (C: Users Desktop User Node backend node_modules ejs lib ejs.js:485:10) At View.render (C: Users Desktop User Node backend node_modules express lib view.js:135:8) at tryRender (C: Users Desktop User Node backend node_modules express lib application.js:640:10) At function.render (C: Users Desktop User Node backend node_modules express lib application.js:592:3) At serverresponse.render (C: Users Desktop User Node backend node_modules express lib Response.js:1008:7) AT: Users Desktop User Node backend Routes index.js:7:12 At layer.Handle [as handle_request] (C: Users Desktop User Node backend node_modules express lib router layer.js:95:5)

  • complementing below, IMPORTANT you learn to use the Bugger to inspect your mistakes. For example, the above error clearly says "people are empty", so you analyze the code step by step to know why the variable is empty. I recommend exploring the VS Code debug options (and using this editor if you’re not already doing it)

1 answer

0

JCSR,

On your GET, you did it this way:

response.render('index', { title: 'Node.js com Express' });

But your index expects to receive pessoas (same as empty), and you only sent in the POST, to correct this situation, you must send pessoas both in GET and POST (correct):

response.render('home', { title: 'Node.js com Express', pessoas: pessoas});

Or even send empty, depends on your logic/implementation:

response.render('home', { title: 'Node.js com Express', pessoas: []});

This already fixes your error and your page will be rendered.


There are other minor problems, your H1 tag is incorrect:

h1>Cadastro em tempo de execução</h1>

Just open the tag correctly:

<h1>Cadastro em tempo de execução</h1>

In your case, you did not declare the variable i:

<% for (i=0; i<pessoas.length; i++) { %>

Simply declare the variable with Let:

<% for (let i=0; i<pessoas.length; i++) { %>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.