我有mongoose的用户名和密码,我使用这个url连接mongodb使用mongoose:
我尝试了这个:
var mongoose = require('mongoose');
mongoose.Promise = global.Promise;
mongoose.connect('mongodb://adminuser:123456@localhost:2017/mydb');
mongoose模式是什么?
// 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;
我的控制器是
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!');
});
});
但这并没有存储在数据库中。
mongodb://adminuser:123456@localhost:2017/mydb'
i think port is wrong, should be27017
mongodb://adminuser:123456@localhost:27017/mydb'
- Subburaj