I can not relate MongoDB to Node

0

I'm trying to make a page that will have a form written Name and Comment, in which when the person fill in the name and the comment will appear underneath the formular.Só que ta giving error. Below this my file EJS and Just below the server file along with Mongo, the routes

<!DOCTYPE html>
<html>
<head>
	<title>X-Sports</title>
	<meta charset="utf-8">
	<link rel="stylesheet" type="text/css" href="/css/quemsomoss.css">
</head>
<body>
<nav id="men">
		<ul>
			<li><picture><img class="pho" src="http://i.imgur.com/rjtgdhD.png"></picture></li><li><ahref="/Paginajava">Home</a></li>
			<li><a href="/quemsomos">Quem somos?</a></li>
			<li><a href="/Produtos">Produtos</a></li>
			<li><a href="/faleconosco">Fale Conosco</a></li>
			<li><form>
	<input type="search" placeholder="Buscar">
</form></li>
		</ul>
</nav>
<section>
<br>
<br>
<br>
<div class="tab">
<h1>Comentários</h1>
<form action="/quemsomos" method ='post'>
	Nome:<input type:"text" name="Nome">
	Comentarios:<input type='text' name="Coment"><br>
	<input type="submit" value="Cadastrar">
</form>

<table>
	<% for(var i=0; i<resultado.length; i++){ %>
	<tr>
		<td><%= resultado[i].Nome %></td>
		<td><%= resultado[i].Comentarios %></td>
	</tr>
	<%}%>
	 
</table>
</div>
</section>


</body>



</html>

And here is that of my server, along with Mongo

var http = require('http');
var express = require("express");
require("ejs");
var bodyparser = require('body-parser');
var mongoose = require("mongoose");

var app = express();


var Post = mongoose.Schema({
    Nome: "String",
    Comentários: "String",
});

var Coment = mongoose.model('Coment', Post);

var mongo = mongoose.connect('mongodb://localhost/Comentarios');

app.use(express.static('./public'));
app.use(express.static('./app/views'));
app.set('views' , './app/views');
app.set('view engine','ejs');
app.use(bodyparser());


app.get('/', function(req, res) {
    res.render('Paginajava');

    });
app.get('/Paginajava', function(req, res) {
     res.render('Paginajava');

    });

app.get('/quemsomos', function(req, res) {
        res.render('quemsomos');
    });

app.post('/quemsomos', function(req,res){
     var novoPost = new Coment(
        {   Nome: req.body.Nome,
            comentarios:req.body.comentarios,
        }
    );

    novoPost.save();
    resp.render("quemsomos");
    resp.end();
});

app.get("/quemsomos", function(req, resp){
    Coment.find(function(err, resultado){
        resp.render("quemsomos", { resultado: resultado} );
        resp.end();
    });

});

app.get('/produtos', function(req, res) {
    res.render('Produtos');



    });

app.get('/faleconosco', function(req, res) {
        res.render('faleconosco');
    });

var meuServidor=http.createServer(app);
meuServidor.listen(8080);

console.log("Servidor Rodando" );

    
asked by anonymous 27.05.2017 / 02:12

2 answers

0

There are some problems with your code, I tried to solve the ones that generate errors ...;)

  

You are statically serving the /app/views folder, which, at first, should only be used by engine (I commented this section):

// app.use(express.static('./app/views'));
  

Next, you have a app.get of page quemsomos , which is preventing the second app.get below rendering (also commented this section):

/*
app.get('/quemsomos', function(req, res) {
        res.render('quemsomos');
    });
*/
  

In your app.post , you're setting in the res function, and trying to call resp - but that's fine, remove the two lines, and replace with res.redirect('/quemsomos'); :

app.post('/quemsomos', function(req,res){
     var novoPost = new Coment(
        {
            Nome: req.body.Nome,
            comentarios:req.body.comentarios,
        }
    );
    novoPost.save();
    res.redirect('/quemsomos');
    // res.render("quemsomos"); // removido
    // res.end(); // removido
});
  

Your schema is using Nome and Comentários as a key - and its app.post , is comentarios , no accent and lowercase letter. Take the accent, and leave both with a capital letter, to stay as Nome . Also, in view , you are using Coment and not comentarios - I changed in server not to touch view , but I suggest the opposite.

var novoPost = new Coment(
    {   
        Nome: req.body.Nome,
        Comentarios: req.body.Coment, // <- é Coment na view
    }
);
  

Remove the accent here:

var Post = mongoose.Schema({
    Nome: "String",
    Comentarios: "String", // <- estava com acento
});
  

Follow the code changed below - but all pages that were not part of the DB interaction were removed - you'd better follow the step-by-step instructions above. The idea is that it works without having to tinker with view (for testing):

var http = require('http');
var express = require("express");
require("ejs");
var bodyparser = require('body-parser');
var mongoose = require("mongoose");
var app = express();

var Post = mongoose.Schema({
    Nome: "String",
    Comentarios: "String",
});

var Coment = mongoose.model('Coment', Post);
var mongo = mongoose.connect('mongodb://localhost/Comentarios');

app.use(express.static('./public'));
app.set('views' , './app/views');
app.set('view engine','ejs');
app.use(bodyparser());

app.post('/quemsomos', function(req,res){
     var novoPost = new Coment(
        {   Nome: req.body.Nome,
            Comentarios: req.body.Coment,
        }
    );
    novoPost.save();
    res.redirect('/quemsomos');
});

app.get("/quemsomos", function(req, resp){
    Coment.find(function(err, resultado){
        resp.render("quemsomos", { resultado: resultado} );
        resp.end();
    });
});

var meuServidor=http.createServer(app);
meuServidor.listen(8080);
console.log("Servidor Rodando" );
  

Posts working:

    
29.05.2017 / 08:20
0

I drop a part of the code, since it did not pass the error. Have you created a schema?

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var commentSchema = new Schema({
  Nome: { String, , required: true }
  comentarios: { type: String, required: true}
});

The link below shows an example of how to do it. link Tell me if it worked .. abs

    
27.05.2017 / 17:57