无法读取未定义的属性(读取 ‘findAll’)

huangapple go评论72阅读模式
英文:

Cannot read properties of undefined (reading 'findAll')

问题

我正在尝试从我的MySQL数据库导入数据到我的index.ejs文件,但它一直报错,我尝试过纠正它,但无法成功,我的index.ejs文件无法识别ejs语法。

这是用express生成路由的文件:

const express = require('express');
const app = express();
const fs = require('fs');
const db = require('./models/db');
const path = require('path');

// 设置视图文件夹的路径
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

// 使用静态文件(CSS、JS等)
app.use(express.static(path.join(__dirname, "public")));

// 发送数据到前端
app.get("/", async (req, res) => {
  try {
    const usuario = await db.usuario.findAll();
    res.render('index', { usuario });
  } catch (error) {
    console.error('Error fetching products:', error);
    res.status(500).send('Internal Server Error');
  }
});

// 接收数据并发送到数据库
app.post("/imput", async (req, resp) => {
  res.send;
});

// 监听本地8080端口
app.listen(8080, () => {
  console.log("Server started on port 8080: http://localhost:8080");
});

这是我的index.ejs文件的部分内容:

<!-- ... -->
<label for="responsavel">Responsável:</label>
<select name="responsavel" id="responsavel" autofocus >
  <% usuario.forEach( (usuario) => { %>
    <option value="<%= usuario.id %>"><%= usuario.nome %></option>
  <% }); %>
</select>
<!-- ... -->

我希望数据能够加载到选择框中。

英文:

I'm trying to import data from my mysql database to my index.ejs file, but it keeps giving me several errors, I've tried to correct it and I couldn't, my index.ejs file is not recognizing the ejs syntax.

this is my file to generate the routes with express

const express = require(&#39;express&#39;);
const app = express();
const fs = require(&#39;fs&#39;);
const db = require(&#39;./models/db&#39;);
const path = require(&#39;path&#39;);

//para colocar conte&#250;do din&#226;minco no html
app.set(&#39;views&#39;, path.join(__dirname, &#39;views&#39;));
app.set(&#39;view engine&#39;, &#39;ejs&#39;);



//para utilizar arquivos est&#225;ticos no node (css, js)
app.use(express.static(path.join(__dirname,&quot;public&quot;)));

//manda infos para o front
 app.get(&quot;/&quot;, async (req, res) =&gt; {

    try{
      const usuario = await db.usuario.findAll();

      res.render(&#39;index&#39;, { usuario });
    }
    catch (error) {
      console.error(&#39;Erro ao buscar produtos:&#39;, error);
      res.status(500).send(&#39;Erro interno do servidor&#39;);
    }


 });

 //recebe infos e manda para o banco de dados
 app.post(&quot;/imput&quot;, async (req, resp) =&gt; {
   res.send
 })

 //se conecta a porta 8080 do localhost
 app.listen(8080, () =&gt; {
    console.log(&quot;servidor iniciado na porta 8080: http://localhost:8080&quot;);
 });

and this is my index.ejs

 &lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
&lt;meta charset=&quot;UTF-8&quot;&gt;
&lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;
&lt;title&gt;Cadastro&lt;/title&gt;
&lt;link rel=&quot;shortcut icon&quot; href=&quot;https://engixelevadores.com.br/public/template/assets/images/favicon/favicon.png&quot; type=&quot;image/x-icon&quot;&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;./style/style.css&quot;&gt;
&lt;script src=&quot;script.js&quot;&gt; &lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div class=&quot;container&quot;&gt;
&lt;h1&gt;Cadastro&lt;/h1&gt;
&lt;!--Forms de cadastro de infos dos clientes, utilizando de uma api para facilitar o preenchimento do endere&#231;o--&gt;
&lt;form id=&quot;addresForm&quot; action=&quot;/submit&quot; method=&quot;post&quot;&gt;
&lt;div class=&quot;infos1&quot;&gt;
&lt;label for=&quot;codigo&quot;&gt;C&#243;digo: &lt;br&gt;&lt;/label&gt;
&lt;input type=&quot;text&quot; name=&quot;codigo&quot; id=&quot;codigo&quot; required&gt; 
&lt;br&gt;
&lt;label for=&quot;nome&quot;&gt;Nome: &lt;br&gt;&lt;/label&gt;
&lt;input type=&quot;text&quot; name=&quot;nome&quot; id=&quot;nome&quot; required&gt;
&lt;br&gt;
&lt;label for=&quot;cpf_cnpj&quot;&gt;CPF/CNPJ: &lt;br&gt;&lt;/label&gt;
&lt;input type=&quot;number&quot; name=&quot;cpf_cnpj&quot; id=&quot;cpf_cnpj&quot; placeholder=&quot;Apenas n&#250;meros&quot;&gt;
&lt;br&gt;
&lt;label for=&quot;pf_pj&quot;&gt;Tipo: &lt;br&gt;&lt;/label&gt;
&lt;label&gt;
&lt;input type=&quot;radio&quot; name=&quot;pf_pj&quot; id=&quot;pf_pj&quot; value=&quot;pf&quot;&gt;
PF
&lt;/label&gt;
&lt;label&gt;
&lt;input type=&quot;radio&quot; name=&quot;pf_pj&quot; id=&quot;pf_pj&quot; value=&quot;pj&quot;&gt;
PJ
&lt;br&gt;&lt;br&gt;
&lt;/label&gt;
&lt;label for=&quot;onde_encontrou_emp&quot;&gt;Como conheceu a Engix? &lt;br&gt;&lt;/label&gt;
&lt;label for=&quot;onde_encontrou_emp&quot;&gt;
&lt;input type=&quot;radio&quot; name=&quot;onde_encontrou_emp&quot; id=&quot;onde_encontrou_emp&quot; value=&quot;Indica&#231;&#227;o&quot;&gt;
Indica&#231;&#227;o
&lt;br&gt;
&lt;/label&gt;
&lt;label for=&quot;onde_encontrou_emp&quot;&gt;
&lt;input type=&quot;radio&quot; name=&quot;onde_encontrou_emp&quot; id=&quot;onde_encontrou_emp&quot; value=&quot;Redes Sociais&quot;&gt;
Redes Sociais
&lt;br&gt;
&lt;/label&gt;
&lt;label for=&quot;onde_encontrou_emp&quot;&gt;
&lt;input type=&quot;radio&quot; name=&quot;onde_encontrou_emp&quot; id=&quot;onde_encontrou_emp&quot; value=&quot;Google&quot;&gt;
Google
&lt;br&gt;
&lt;/label&gt;
&lt;label for=&quot;onde_encontrou_emp&quot;&gt;
&lt;input type=&quot;radio&quot; name=&quot;onde_encontrou_emp&quot; id=&quot;onde_encontrou_emp&quot; value=&quot;Google&quot;&gt;
Comercial
&lt;br&gt;
&lt;/label&gt;
&lt;label for=&quot;onde_encontrou_emp&quot;&gt;
&lt;input type=&quot;radio&quot; name=&quot;onde_encontrou_emp&quot; id=&quot;onde_encontrou_emp&quot; value=&quot;Outro&quot;&gt;
Outros
&lt;input type=&quot;text&quot; name=&quot;onde_encontrou_emp&quot; id=&quot;onde_encontrou_emp&quot;&gt;
&lt;/label&gt;
&lt;/div&gt;
&lt;div class=&quot;infos2&quot;&gt;
&lt;!--Chama a function pesquisacep que est&#225; no arquivo script.js, ela far&#225; a consulta em um banco de dados externo e autocompletar&#225; os campos abaixo--&gt;
&lt;label for=&quot;cep&quot;&gt;CEP: &lt;br&gt;&lt;/label&gt;
&lt;input type=&quot;text&quot; name=&quot;cep&quot; id=&quot;cep&quot; size=&quot;15&quot; maxlength=&quot;9&quot; onblur=&quot;pesquisacep(this.value);&quot; placeholder=&quot;Apenas n&#250;meros&quot;&gt;
&lt;br&gt;
&lt;label for=&quot;rua&quot;&gt;Rua: &lt;br&gt;&lt;/label&gt;
&lt;input type=&quot;text&quot; name=&quot;rua&quot; id=&quot;rua&quot; size=&quot;50&quot;&gt;
&lt;br&gt;
&lt;label for=&quot;numero&quot;&gt;Numero: &lt;br&gt;&lt;/label&gt;
&lt;input type=&quot;text&quot; name=&quot;numero&quot; id=&quot;numero&quot; size=&quot;50&quot;&gt;
&lt;br&gt;
&lt;label for=&quot;bairro&quot;&gt;Bairro: &lt;br&gt;&lt;/label&gt;
&lt;input type=&quot;text&quot; name=&quot;bairro&quot; id=&quot;bairro&quot; size=&quot;50&quot;&gt;
&lt;br&gt;
&lt;label for=&quot;cidade&quot;&gt;Cidade: &lt;br&gt;&lt;/label&gt;
&lt;input type=&quot;text&quot; name=&quot;cidade&quot; id=&quot;cidade&quot; size=&quot;50&quot;&gt;
&lt;br&gt;
&lt;label for=&quot;uf&quot;&gt;UF: &lt;br&gt;&lt;/label&gt;
&lt;input type=&quot;text&quot; name=&quot;uf&quot; id=&quot;uf&quot; size=&quot;50&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;infos3&quot;&gt;
&lt;label for=&quot;contato_cliente&quot;&gt;Nome do contato: &lt;br&gt;&lt;/label&gt;
&lt;input type=&quot;text&quot; name=&quot;contato_cliente&quot; id=&quot;contato_cliente&quot;&gt;
&lt;br&gt;
&lt;label for=&quot;telefone&quot;&gt;N&#186; Telefone fixo: &lt;br&gt;&lt;/label&gt;
&lt;input type=&quot;tel&quot; name=&quot;telefone&quot; id=&quot;telefone&quot; placeholder=&quot;Apenas n&#250;meros&quot;&gt;
&lt;br&gt;
&lt;label for=&quot;celular&quot;&gt;N&#186; Celular: &lt;br&gt;&lt;/label&gt;
&lt;input type=&quot;tel&quot; name=&quot;celular&quot; id=&quot;celular&quot; placeholder=&quot;Apenas n&#250;meros&quot;&gt;
&lt;br&gt;
&lt;p&gt;Se n&#227;o houver DDD, ele ser&#225; &lt;br&gt; definido automaticamente como (51)&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;infos4&quot;&gt;
&lt;label for=&quot;responsavel&quot;&gt;Respons&#225;vel:&lt;/label&gt;
&lt;select name=&quot;responsavel&quot; id=&quot;responsavel&quot; autofocus &gt;
** &lt;% usuario.forEach( =&gt; { %&gt;
&lt;option value=&quot;&lt;%= usuario.id %&gt;&quot;&gt;&lt;%= usuario.nome %&gt;&lt;/option&gt;
&lt;% }); %&gt;**
&lt;/select&gt;
&lt;br&gt;
&lt;label for=&quot;tipo_equipamento&quot;&gt;Tipo de equipamento:&lt;/label&gt;
&lt;select name=&quot;tipo_equipamento&quot; id=&quot;tipo_equipamento&quot; autofocus &gt;
&lt;option value=&quot;PNE&quot;&gt;PNE&lt;/option&gt;
&lt;option value=&quot;Elevador Comercial&quot;&gt;Elevador Comercial&lt;/option&gt;
&lt;option value=&quot;Elevador Residencial&quot;&gt;Elevador Residencial&lt;/option&gt;
&lt;option value=&quot;N&#227;o informado&quot;&gt;N&#227;o informado&lt;/option&gt;
&lt;/select&gt;
&lt;br&gt;
&lt;label for=&quot;tipo_equipamento&quot;&gt;Tipo de equipamento:&lt;/label&gt;
&lt;select name=&quot;tipo_equipamento&quot; id=&quot;tipo_equipamento&quot; autofocus &gt;
&lt;option value=&quot;PNE&quot;&gt;PNE&lt;/option&gt;
&lt;option value=&quot;Elevador Comercial&quot;&gt;Elevador Comercial&lt;/option&gt;
&lt;option value=&quot;Elevador Residencial&quot;&gt;Elevador Residencial&lt;/option&gt;
&lt;option value=&quot;N&#227;o informado&quot;&gt;N&#227;o informado&lt;/option&gt;
&lt;/select&gt;
&lt;/div&gt;
&lt;/form&gt;
&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;

was for the data to be loaded in the select

答案1

得分: 0

从您提供的代码来看,似乎您正试图渲染index.ejs文件并将usuario数据传递给它。但是,看起来在您的index.ejs文件中存在语法问题,您尝试对usuario数组进行迭代。

当前

<% usuario.forEach( => { %>
<option value="<%= usuario.id %>"><%= usuario.nome %></option>
<% }); %>

修正

<% usuario.forEach((user) => { %>
<option value="<%= user.id %>"><%= user.nome %></option>
<% }); %>
英文:

From the code you provided, it seems like you are trying to render the index.ejs file and pass the usuario data to it. However, it looks like there is an issue with the syntax in your index.ejs file where you're trying to iterate over the usuario array.

Current:

&lt;% usuario.forEach( =&gt; { %&gt;
&lt;option value=&quot;&lt;%= usuario.id %&gt;&quot;&gt;&lt;%= usuario.nome %&gt;&lt;/option&gt;
&lt;% }); %&gt;

Fixed

&lt;% usuario.forEach((user) =&gt; { %&gt;
&lt;option value=&quot;&lt;%= user.id %&gt;&quot;&gt;&lt;%= user.nome %&gt;&lt;/option&gt;
&lt;% }); %&gt;

huangapple
  • 本文由 发表于 2023年7月13日 19:53:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76679081.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定