0

I have got the mongo db commands from the below link.

http://try.mongodb.org/

I have a project and I am able to run it. In the project we use mongodb, angular js, node js and express server.

I am able to create user from the login page.The users are stored in mongo db. I would like to know what are the users in the database.

Initially I did not install any mongodb. But some how, the user and password was stored in localhost. I hope my project will be having certain libraries.

But later, in order to learn mongo, i installed mongo to my PC. I started the mongo db and it is running successfully in the link

http://localhost:28017/. 
MongoDB starting : pid=6300 port=27017 dbpath=\data\db\ 64-bit host=new-PC
  • Question 1

I would like to list out the users in the db, that is used by my project. Will installation of mongo db help me in some way? If not without starting mongo db, can i know where are the user details saved by my project?

  • Question 2

How can I set up mongo db in remote so that it is available in the internet?


Code

 - Creating UserSchema

var UserSchema = new Schema({ name: String, email: { type: String, unique: true }, hashed_password: String, provider: String, salt: String, facebook: {}, twitter: {}, github: {}, google: {} });

mongoose.model('User', UserSchema);

 - Code for creating user

var mongoose = require('mongoose'), User = mongoose.model('User'), q = require('q'), _ = require('lodash');

exports.create = function(req, res, next) { var reqUser = new User(req.body); var email = reqUser.email; if(!reqUser.email) { res.send(400, {message: 'Email is mandatory!'}); }

var message = null;
reqUser.provider = 'local';
var savePromise = q.when(User.findOne({'email': reqUser.email}).exec());
savePromise.then(function(user) {
    return reqUser;
    if(user == null)
    {
        return reqUser;
    } else
    {
        if(reqUser.provider!=user.provider && !user.hashed_password)
        {
            user.password = reqUser.password;
        } else {
            throw new Error("User alredy exists!");
        } 
        console.log("In Else");
        console.log(user);
        return user;
    }
}, function(err){
        console.log("In Error 1");
    }
).then(function(user){
    console.log("In then 2");
    console.log(user);
    return user.save();
}, function(err){
    console.log("In error 2");
    console.log(err);   
}).then(function(obj){
    console.log("In then 3");
    console.log(obj);
}).fail(function(error){
    console.log("In Fail");
    console.log(error);
}).done(function(){
   // console.log(user);
    console.log("in done");
});
console.log("Returning 400 at the end");

user.save(function(err) {
    if (err) {
        console.log(err);
        message = 'Unknowd error occured, please try again!';
        if(err.code=11000)
        {
            message = 'User with this email already exist. Existing user? please login <a href="/signin">here</a>';
            User.findOne({
                    email: user.email
                }, function(err, dbuser) {
                    if (err) {
                        return res.json(400, { message: message });
                    }else {
                        if(dbuser.provider!=user.provider)
                        {
                            console.log("In If");
                            console.log(dbuser);
                            if(!dbuser.hashed_password)
                            {
                                console.log("In another if");
                                dbuser.password = user.password;
                                dbuser.save(function(err){
                                    if(err)
                                    {
                                        console.log(err);
                                    }
                                    return res.json(dbuser);
                                });
                            }else
                            {
                                console.log("Returning error");
                                return res.json(400, { message: message })
                            }
                        } else
                        {
                            return res.json(400, { message: message });
                        }            
                    }
                });
        }

    }else
    {
        return res.json(user);
    }
    //console.log("Returning success");

});
};

Thanks, Sabarisri

sabari
  • 2,595
  • 5
  • 28
  • 44

2 Answers2

1

To answer question #2 first, you will need either an instance of mongoDB on your own server or get a hosted one.

See for example Database-as-a-service. There are quite a lot of possibilites, check out some other offers.

Your first question is confusing me. It would be helpful to see the code that creates a user.

Wottensprels
  • 3,307
  • 2
  • 29
  • 38