Node.js 如何在节点js的mongoose数据库中添加新用户
我正在学习thiecourse“通过构建10个项目学习节点”。我的编码方式与我的指导老师相同,但当我向mongo数据库添加新用户时,除了Node.js 如何在节点js的mongoose数据库中添加新用户,node.js,mongoose,mangodb,Node.js,Mongoose,Mangodb,我正在学习thiecourse“通过构建10个项目学习节点”。我的编码方式与我的指导老师相同,但当我向mongo数据库添加新用户时,除了{“\uu v”:0} var mongoose = require('mongoose'); mongoose.connect('mongodb://localhost/nodeauth'); var db = mongoose.connection; // User Schema var UserSchema = mongoose.Schema({
{“\uu v”:0}
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/nodeauth');
var db = mongoose.connection;
// User Schema
var UserSchema = mongoose.Schema({
username: {
type: String,
index: true
},
password: {
type: String,
},
email: {
type: String
},
name: {
type: String
},
profileimage: {
type: String
}
});
var User = module.exports = mongoose.model('User', UserSchema);
module.exports.createUser = function(newUser,callback){
newUser.save(callback);
}
我正在学习thiecourse“通过构建10个项目学习节点”。我的编码方式与我的指导老师相同,但当我向mongo数据库添加新用户时,除了{“\uu v”:0}
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/nodeauth');
var db = mongoose.connection;
// User Schema
var UserSchema = mongoose.Schema({
username: {
type: String,
index: true
},
password: {
type: String,
},
email: {
type: String
},
name: {
type: String
},
profileimage: {
type: String
}
});
var User = module.exports = mongoose.model('User', UserSchema);
module.exports.createUser = function(newUser,callback){
newUser.save(callback);
}
//这就是我创建用户的方式
router.post('/register', function(req, res, next) {
var name = req.body.name;
var email = req.body.email;
var username = req.body.username;
var password = req.body.password;
var password2 = req.body.password2;
var multer = require('multer');
var upload = multer({ dest: './uploads' });
// Check for Image Field
if(req.files && req.files.profileimage){
console.log('uploading File...');
// File Info
var profileImageOriginalName = req.files.profileimage.originalname;
var profileImageName = req.files.profileimage.name;
var profileImageMime = req.files.profileimage.mimetype;
var profileImagePath = req.files.profileimage.path;
var profileImageExt = req.files.profileimage.extension;
var profileImageSize = req.files.profileimage.size;
} else {
// Set a Default Image
var profileImageName = 'noimage.png';
}
var newUser = new User({
name: name,
email: email,
username: username,
password: password,
profileImage: profileImageName
});
// Create User
User.createUser(newUser, function(err, user){
if(err)throw err;
console.log(user);
});
//Success Message
req.flash('success', 'You are now registered and may log in');
res.location('/');
res.redirect('/');
//}
});
请检查代码,看看我哪里出错了,这导致mongoose表示您正在保存一个没有值的用户。 您需要保存一个用户,例如:
let user= new User({
userName: value,
userPassword: value
});
user.save();
或者更详细地展示您如何尝试传递值,以便我们可以检查最佳方法。使用您的自定义方法
createUser
let userModel= new User();
let data = {username:'alpha',password:'beta',name:'gamma'};
userModel.createUser(data,(err,doc) => {
if(err){
console.log(err);
}
console.log('saved');
})
创建后只需添加
newUser.save()
如何调用createUser
?请显示调用createUserim的代码,我已经在这样做了,但我似乎在mongoose dbHello Rahul中得到了空值。我发现您在用户模式中有一个错误,请注意,您保存的回调不是新用户,尝试更改它,看看它是否修复。导出createUser的最后一行:newUser.save(callback),尝试只执行newUser.save();要查看它是否修复,请查看数据是否正在提交?在检查时使用浏览器进行检查,以查看用户是否正在进行post请求。另外,请尝试直接保存,而不是使用您创建的函数。注释代码//Create User.createUser(newUser,function(err,User){if(err)throw err;console.log(User);})
并直接执行newUser.save()
并从变量中执行一些console.log,以查看它们是否也有任何值