-1
I created schema.js to consume data from a small database in Mongodb const Mongoose = require('Mongoosis');
//create schema
var Schema = mongoose.Schema;
var restorationSchema = new Schema({
point_slug: { type: String, default: true },
coordinates: { type: String, default: true },
data: { type: Date, contentType: Number, default: true }
})
restorationSchema.virtual("id").get(function() {
return this._id;
});
// exporto este módulo
const points = mongoose.model("points", restorationSchema);
module.exports = points;
And I climbed a local server to run Node on the backend
const express = require("express");
const points = require("./schema.js");
const bodyParser = require('body-parser');
const db = require("./repository.js");
const PORT = process.env.PORT || 5000;
const MONGO_URL = "mongodb://localhost:27017/restoration";
// padrao: mongodb://dominio:porta/database
db.connect(MONGO_URL)
console.log('connect to mongodb')
const app = express();
app.use(bodyParser.json());
app.get("/points", (req, res) => {
points.find((error, response) => {
// preciso tratar o erro, caso ocorra
if (error) {
// caso tenha algum erro
return res.status(500).send(error);
}
// caso contrário, envio o retorno
res.status(200).send(response);
});
})
app.get("/points/:id", (req, res) => {
points.findById(
req.params.id,
function(err, point) {
if (err) return res.send(err);
if (!point) return res.status(404).send({});
console.log('*** point.id:', point.id)
res.send(point);
}
);
});
app.listen(PORT, () => console.log(`Ouvindo na porta ${PORT}...`));
However, when I access the url to check Collection 'points' data inside the database using the url in localhost//5000/points, I get this error mengam which I do not know how to decipher/understand.
Guy(the)
.send(error);
failed to error the "body" of the HTTP response?– Guilherme Nascimento
I put an image to demonstrate the error,@Guilhermenascimento
– Lilian Guimarães
You did not need an image, you could select and post the error directly, I recommend reading: https://pt.meta.stackoverflow.com/q/7816/3635
– Guilherme Nascimento
Thanks for the tip
– Lilian Guimarães