Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

161
Visualizações
Model.create() from Mongoose doesn´t save the Documents in my Collection

I have created a sigle app with a Schema and a Model to create a Collection and insert some Documents.

I have my todoModel.js file:

const mongoose = require("mongoose");
const Schema = mongoose.Schema;

const todoSchema = new Schema({
username: String,
todo: String,
isDone: Boolean,
hasAttachment: Boolean
});
const Todos = mongoose.model("Todo", todoSchema);
module.exports = Todos;

Then I have created a setUpController.js file with a sample of my Documents. Then I create a Model and I pass my sample of Documents and my Schema. I create a response to send tje result in JSON. Everything good here, as I get the result in json when accessing to the route.

Here is the code:

        Todos.create(sampleTodos, (err, results) => {

        if (!err) {
            console.log("setupTodos sample CREATED!")
            res.send(results);
        }
        else {
            console.log(`Could not create the setupTodos Database sample, err: ${err}`);
        }
        });

My problem is that this Documents don´t get saved in the collection !! When I access to the database, nothing is there.

This is my app.js file:

mongoose.connect("mongodb://localhost:27017/nodeTodo")
.then(connection => {
    app.listen(port);
    
})
.catch(err => {
    console.log(`Could not establish Connection with err: ${err}`);
});

Could anyone help me please ?

Thank you

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can't use object directly without making an instance of it. Try creating an instance and make the respective function call of that instance.In your case, save the document after creating an instance and it works like a charm.

const newTodos = new Todos({
username: "username",
todo: "todos",
isDone: false,
hasAttachment: flase
});

const createdTodo = newTodos.save((err, todo) => {
if(err) {
throw(err);
}
else {
//do your staff
}
})

about 4 years ago · Juan Pablo Isaza Relatório

0

after the collection is created you can use the function inserMany to insert also a single document the function receives an array of objects and automatically saves it to the given collection

example:

  Pet = new mongoose.model("pet",schemas.petSchema)
  Pet.insetMany([
    {
      //your document
    }])

it will save only one hardcoded document

I hope it was helpful

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda