我使用Nodejs和mongodb来练习开发网站,但我遇到了一个问题。我的server.js代码如下:
var express=require('express');
var morgan=require('morgan');
var mongoose=require('mongoose');
var bodyParser=require('body-parser');
var User=require('./models/user');
var app=express();
mongoose.connect('mongodb://root:abc123@ds021172.mlab.com:21172/test', function(err){
if(err){
console.log(err);
} else{
console.log("Connected to the database");
}
});
//Middleware
app.use(morgan('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true}));
app.post('/create-user', function(req, res, next){
var user=new User();
user.profile.name=req.body.name;
user.password=req.body.password;
user.email=req.body.email;
user.save(function(err){
if(err) return next(err);
res.json('Successfully created a new user');
});
});
app.listen(3000, function(err){
if(err) throw err;
console.log("Server is running on port 3000");
});
我的user.js代码如下:
var mongoose=require('mongoose');
var bcrypt=require('bcrypt-nodejs');
var Schema=mongoose.Schema;
/* The user schema attribution / chatacteristics / field */
var UserSchema = new mongoose.Schema({
email: {type: String, unique: true, lowercase: true},
password: String,
profile: {
name: {type: String, default: ''},
picture: {type: String, default: ''}
},
address: String,
history: [{
date: Date,
paid: {type: Number, default: 0},
//item{type: Schema.Types.ObjectId, ref: ''}
}]
});
/* Hash the password before we even save it to the datebase */
UserSchema.pre('save', function(next){
var user=this;
if(!user.isModified('password')) return next();
bcrypt.genSalt(10, function(err, salt){
if(err) return next(err);
bcrypt.hash(user.password, salt, null, function(err, hash){
if(err) return next(err);
user.password=hash;
next();
});
});
});
/* compare password in the database and the one that the user type in */
UserSchema.methods.comparePassword=function(password){
return bycrypt.compareSync(password, this.password);
}
module.exports=mongoose.model('User', UserSchema);
但是当我使用邮递员尝试发布如下数据时:
我输入三个参数,名称,密码,电子邮件,然后点击发布按钮,我收到错误:
TypeError: User is not a function
<br> at /Users/chengyuanying/ecommerce/server.js:27:12
<br> at Layer.handle [as handle_request] (/Users/chengyuanying/ecommerce/node_modules/express/lib/router/layer.js:95:5)
<br> at next (/Users/chengyuanying/ecommerce/node_modules/express/lib/router/route.js:131:13)
<br> at Route.dispatch (/Users/chengyuanying/ecommerce/node_modules/express/lib/router/route.js:112:3)
<br> at Layer.handle [as handle_request] (/Users/chengyuanying/ecommerce/node_modules/express/lib/router/layer.js:95:5)
<br> at /Users/chengyuanying/ecommerce/node_modules/express/lib/router/index.js:277:22
<br> at Function.process_params (/Users/chengyuanying/ecommerce/node_modules/express/lib/router/index.js:330:12)
<br> at next (/Users/chengyuanying/ecommerce/node_modules/express/lib/router/index.js:271:10)
<br> at urlencodedParser (/Users/chengyuanying/ecommerce/node_modules/body-parser/lib/types/urlencoded.js:95:37)
<br> at Layer.handle [as handle_request] (/Users/chengyuanying/ecommerce/node_modules/express/lib/router/layer.js:95:5)
<br> at trim_prefix (/Users/chengyuanying/ecommerce/node_modules/express/lib/router/index.js:312:13)
<br> at /Users/chengyuanying/ecommerce/node_modules/express/lib/router/index.js:280:7
<br> at Function.process_params (/Users/chengyuanying/ecommerce/node_modules/express/lib/router/index.js:330:12)
<br> at next (/Users/chengyuanying/ecommerce/node_modules/express/lib/router/index.js:271:10)
<br> at jsonParser (/Users/chengyuanying/ecommerce/node_modules/body-parser/lib/types/json.js:107:37)
<br> at Layer.handle [as handle_request] (/Users/chengyuanying/ecommerce/node_modules/express/lib/router/layer.js:95:5)
你可以帮我解决这个问题吗?
答案 0 :(得分:0)
我更改了这一行:
var UserSchema = new Schema({
在user.js文件中,因此,它修复了问题。但我不知道为什么