Hello Dev’s
I’m try to solve the “MongoDB and Mongoose - Create a Model” exercise and don’t have success.
I read the questions about this exercise, but the message that i receive is: “not found”
Anybody can help me, to identify what is wrong with the code above?
Thanks in advance.
My Code:
var express = require('express');
var app = express();
var mongoose = require('mongoose');
app.get("/", function(req, res, next)
{
if(process.env.MONGODB_URI)
{
mongoose.connect(process.env.MONGODB_URI);
// connect to the server
var db = mongoose.connection;
// check connection errors
db.on('error', console.error.bind(console, 'connection error:'));
// Once established connection
db.once('open', function(done)
{
var Schema = mongoose.Schema;
var PersonSchema = new Schema(
{
name:
{
type: String,
required: true
},
age: Number,
favoriteFoods :[ String]
});
var PersonModel = mongoose.model('Person', PersonSchema);
PersonModel.create({ name: 'Teste', age: 18, favoriteFoods: ['Pao','Hamburger'] }, function (err, small)
{
if (err) return res.end(err);
done();
});
});
}
});