使用Node.js和Mangoose将记录插入集合时,出现以下错误。
错误:
OverwriteModelError: Cannot overwrite `alloca_temp` model once compiled.
at new OverwriteModelError (/home/subrajyoti/Desktop/landing/node_modules/mongoose/lib/error/overwriteModel.js:20:11)
at NativeConnection.Connection.model (/home/subrajyoti/Desktop/landing/node_modules/mongoose/lib/connection.js:776:13)
at exports.createCollections (/home/subrajyoti/Desktop/landing/controller/controller.js:162:19)
at Layer.handle [as handle_request] (/home/subrajyoti/Desktop/landing/node_modules/express/lib/router/layer.js:95:5)
at next (/home/subrajyoti/Desktop/landing/node_modules/express/lib/router/route.js:131:13)
at Route.dispatch (/home/subrajyoti/Desktop/landing/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/home/subrajyoti/Desktop/landing/node_modules/express/lib/router/layer.js:95:5)
at /home/subrajyoti/Desktop/landing/node_modules/express/lib/router/index.js:277:22
at Function.process_params (/home/subrajyoti/Desktop/landing/node_modules/express/lib/router/index.js:330:12)
at next (/home/subrajyoti/Desktop/landing/node_modules/express/lib/router/index.js:271:10)
at methodOverride (/home/subrajyoti/Desktop/landing/node_modules/method-override/index.js:65:14)
at Layer.handle [as handle_request] (/home/subrajyoti/Desktop/landing/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/home/subrajyoti/Desktop/landing/node_modules/express/lib/router/index.js:312:13)
at /home/subrajyoti/Desktop/landing/node_modules/express/lib/router/index.js:280:7
at Function.process_params (/home/subrajyoti/Desktop/landing/node_modules/express/lib/router/index.js:330:12)
at next (/home/subrajyoti/Desktop/landing/node_modules/express/lib/router/index.js:271:10)
我在下面提供我的代码。
mongoose.connect(url+"/"+database,{ useNewUrlParser: true });
var db = mongoose.connection;
db.on("error", console.error.bind(console, "connection error"));
db.once("open", function(callback) {
console.log("Connection succeeded.");
});
//console.log('db1',db);
var Schema = mongoose.Schema;
exports.createCollections=function(req,res){
var userSchema=new Schema({
login_id: String,
password: String,
full_name: String,
user_type: Number,
role_type: String,
allocated_to: Number,
zone: Number,
state: Number,
location: Number,
is_active: Number,
added_on: Date,
device_id: { type: String, default:"" },
access_key: String
})
var tempmodel=db.model('alloca_temp', tempSchema);
tempmodel.find({},function(err,docs){
//console.log('docs',docs);
//res.send(docs);
tempArr=docs;
//res.send(tempArr);
var user = db.model("User", userSchema);
var password='123456';
for(var i=0;i<tempArr.length;i++){
var haspass='';
bcrypt.genSalt(10, function(err, salt) {
bcrypt.hash(password, salt, function(err, hash) {
haspass=hash;
});
});
var data='';
data={
login_id:tempArr[i]['login_mobile_no'],
password:haspass,
full_name: tempArr[i]['customer_name'],
user_type: 4,
role_type: 'AGENCY FOS',
allocated_to: tempArr[i]['allocated_to'],
zone: tempArr[i]['zone'],
state: tempArr[i]['state'],
location: tempArr[i]['location'],
is_active: 1,
added_on: todaydate(),
device_id: "",
access_key: ""
}
var use=new user(data);
use.save(function(err,user){
if (!err) {
console.log('id',user._id);
}
})
}
})
})
在这里,我从一个集合中获取值,需要插入另一个集合中,但出现此错误。