GET method does not access data in mongoDB

Asked

Viewed 74 times

-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.

inserir a descrição da imagem aqui

  • Guy(the) .send(error); failed to error the "body" of the HTTP response?

  • I put an image to demonstrate the error,@Guilhermenascimento

  • 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

  • Thanks for the tip

1 answer

0


I believe you tried to define true in a scheme that expected a value of the kind Date:

data: { type: Date, contentType: Number, default: true }

Probably what you want is the "current date", so set in default the Date.now:

data: { type: Date, contentType: Number, default: Date.now }
  • I changed the schema according to your suggestion. Access URL localhost://5000/points and gives GET error inda, I don’t know what I can be

  • I tweaked it and got it, thank you very much =)

  • @Lilianguimarães please mark the answer as correct. Thank you!

Browser other questions tagged

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