大家好,我真的需要帮助。这已经困扰了我多少天
模型/ user.js的
var UserSchema = mongoose.Schema({
username:{
type: String,
unique: true,
index:true
},
password:{
type:String
},
email:{
type:String,
required: true
// unique: true
},
authToken:{
type: String,
required: true,
unique: true
},
IsAuthenticated:{
type: Boolean,
required: true
},
name:{
type:String
},
field:{
type:String
},
e_money:{
type:Number //this is the integer form in mongoose
}
});
//accesible variable from the outside
var User = module.exports = mongoose.model('users', UserSchema);
var InfoUser = module.exports = mongoose.model('infouser', UserSchema);
以及如何保存就像这样
var User = require('../models/user);
var newUser = new User({
name: name,
email: email,
authToken: authToken,
IsAuthenticated: false,
username: username,
password: password,
field: field,
e_money: e_money //temporary emoney
});
var newUser2 = new InfoUser({
name: name,
email: email,
authToken: authToken,
IsAuthenticated: false,
username: username,
password: password,
field: field,
e_money: e_money //temporary emoney
});
//save the newly created user to database
User.createUser(newUser,function(err, user){
if(err) throw err;
console.log(user);
)};
User.createUser(newUser2,function(err,user){
if(err) throw err;
console.log(user);
)};
问题是什么总是说没有定义infouser。有人可以玩吗
答案 0 :(得分:2)
问题是您通过相同的module.exports
导出两个不同的模型。相反,我建议您单独导出它们:
<强>模型/ user.js的强>
// You can add instance methods like this:
UserSchema.methods.createUser = function(user) {
// Whatever you want to do here
};
var User = mongoose.model('users', UserSchema);
var InfoUser = mongoose.model('infouser', UserSchema);
exports.User = User;
exports.InfoUser = InfoUser;
/*
You could also do this as:
module.exports = { User: User, InfoUser: InfoUser };
*/
然后当你想要使用它们时:
var User = require('../models/user').User;
var InfoUser = require('../models/user').InfoUser;