Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't MongoDB save my properties

I'm populating my DB with some dummy data where I'm trying to add a user. A user object is created but none of the properties are save... whats up?

app.get('/setup', function (req, res) {

    User.findOne({ name: "Nick" }, function (err, user) {

        if (user == undefined) {

            var nick = new User({
                name: "Nick",
            }).save();

            res.json({ "success": true, "msg": "user created" });
        } else {
            res.json({ "success": true, "msg": "user existed" });
        }
    });
});

calling this returns "user created". Here is my method to output all users:

app.get('/users', function(req, res) {

    User.find({}, function(err, user) {
        res.json(user);
    });
});

The output here is

[
  {
    "_id": "565772db5f6f2d1c25e999be",
    "__v": 0
  },
  {
    "_id": "5657734ba859fefc1dca77db",
    "__v": 0
  },
  {
    "_id": "5657738ba859fefc1dca77dc",
    "__v": 0
  },
  {
    "_id": "565774f1cf99a2b81fca1e7f",
    "__v": 0
  },
  {
    "_id": "565775f0cf99a2b81fca1e80",
    "__v": 0
  }
]

Where I've tried to add "Nick" a couple of times noe... any input? :D

My Mongoose model, located in its own Model.js file with other models:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

// set up a mongoose model and pass it using module.exports
module.exports = mongoose.model('User', new Schema({ 
    name: String, 
    password: String, 
}));
like image 544
Jason94 Avatar asked Nov 26 '15 21:11

Jason94


People also ask

How do I save changes in MongoDB?

MongoDB's update() and save() methods are used to update document into a collection. The update() method updates the values in the existing document while the save() method replaces the existing document with the document passed in save() method.

What does save () in MongoDB return?

The save() returns a WriteResult() object that contains the status of the insert or update operation.

Is Mongoose good for MongoDB?

The benefit of using Mongoose is that we have a schema to work against in our application code and an explicit relationship between our MongoDB documents and the Mongoose models within our application. The downside is that we can only create blog posts and they have to follow the above defined schema.

How does node save data in MongoDB?

To save the data into the database, we need to create a new instance of our model that we created early. We will pass into this instance the user's input. Once we have it then we just need to enter the command “save”. Mongoose will return a promise on a save to the database.


2 Answers

Given you have a name in you user Schema, you also need to wait until your save() query is finished and then send a response:

app.get('/setup', function (req, res) {
    User.findOne({ name: "Nick" }, function (err, user) {
        if (err) {
          return res.json({ "success": false, "error": err });
        }

        if (user) {
          return res.json({ "success": true, "msg": "user existed" });
        }

        var nick = new User({ name: "Nick" });
        nick.save(function(err, userObj) {
           if (err) {
             return res.json({ "success": false, "error": err });
           }

           res.json({ "success": true, "msg": "user created" });
        });
    });
});
like image 103
Lisa Gagarina Avatar answered Sep 30 '22 14:09

Lisa Gagarina


please try with this code :

app.get('/setup', function (req, res) {

    User.findOne({ name: "Nick" }, function (err, user) {

        if (!err && user) {
                res.json({ "success": true, "msg": "user existed" });

        } 
        else {

            var nick = new User({name: "Nick" });
            nick.save(function(err){
               if(!err)
                   res.json({ "success": true, "msg": "user created" });
            })

        }
    });
});
like image 23
Bilash Avatar answered Sep 30 '22 14:09

Bilash