0
私はNodejsとmongodbを使ってWebサイトの開発を実践していますが、問題はありました。私server.jsコードは以下の通りです:postman typeerrorユーザーは機能ではありません
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:[email protected]: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);
が、私は郵便配達を使用します以下のようなデータを投稿しようとします:
私は3つのパラメータ名、パスワード、メールアドレスを入力して、ポスト]ボタンをクリックして、私はエラーを得た:
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)
あなたは私がこの問題を解決するのに役立ちます問題?