How to connect mongodb(mongoose) with username and password?

parithi info

I have username and password for mongoose, i used this url to connect the mongodb using mongoose

I try this one :

var mongoose = require('mongoose');
mongoose.Promise = global.Promise;
mongoose.connect('mongodb://adminuser:123456@localhost:2017/mydb');

mongoose schema is

  // grab the things we need
  var mongoose = require('mongoose');
  var Schema = mongoose.Schema;

  // create a schema
  var userSchema = new Schema({
  name: String,
  username: { type: String, required: true, unique: true },
  password: { type: String, required: true },
  admin: Boolean,
  location: String,
  meta: {
  age: Number,
  website: String
  },
  created_at: Date,
  updated_at: Date
  });

  // the schema is useless so far
  // we need to create a model using it
  var User = mongoose.model('User', userSchema);

  // make this available to our users in our Node applications
  module.exports = User;

and my controller is

    router.post('/signup',function(req,res){
     console.log("Inside")
     var useR= new User({
      name: 'Chris',
      username: 'sevilayha',
      password: 'password' 
     });
     useR.save(function(err) {
      if (err) throw err;
      console.log('User saved successfully!');
     });
    });

but its not stored in database .

Sultan Khan

First check whether it connected or node using on event.

mongoose.connect('mongodb://adminuser:123456@localhost:2017/mydb');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function callback () {
  console.log("h");
});

exports.test = function(req,res) {
  console.log(res)
};

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to connect LDAP With username and password?

How to connect to MongoDB 3.2 in Java with username and password?

How to connect with an API that requires username and password

How to connect with coreNLP server with username password?

FTP connect with no username and password?

How to configure MongoDB to be able to connect using username and password?

How to use username and password (no SSL) in Java client to connect to Elasticsearch?

How to automatically enter username and password when prompted to connect to a remote server

How to connect to an ActiveMQ server by setting the username and password programmatically?

Connect to OPCUA server with username and password

connect to openvpn using username and password

orientdb connect invalid password and username

Using JSoup to connect to a website with username/password

Connect to TFS GIT domain\username + password

Delphi/Rdp check username and password before connect

Use windows explorer to connect to a server with username and password

How to store username and password details from my app to my database and how to connect my app with my database?

How to start ssh sever android programmatically, and how i will get username and password for connect to device

How can i connect a Java mqtt client with username and password to an emqttd(EMQ) broker?

How to connect Django's built-in username and password fields to user-defined html input tags

How can i connect a Java mqtt client with username and password paho client

How to connect my mySQL database(username and password) into my javascript for login successfully?

How to secure nodered with username and password

How to recover lost username and password?

Firefox: how to autocomplete password but not username

How to save username and password with Mercurial?

How to secure MongoDB with username and password

How to save username and password in Git?

How to specify username password for ftp