mongoose踩坑笔记: Cannot overwrite `` model once compiled.
出现这个问题是因为
var schema = new mongoose.Schema({ qishu: Number, shengliguojia: Number }); var temp = mongoose.model('yulechang', schema, 'yulechang');
重复定义‘mongoose.model('yulechang', schema, 'yulechang')’这个,写一遍就行(下面是示例:这是改过后的)
var schema = new mongoose.Schema({ qishu: Number, shengliguojia: Number }); var temp = mongoose.model('yulechang', schema, 'yulechang'); mongoose.connect(db_str, { useNewUrlParser: true }, function(err) { if (!err) { temp.find().sort({ "qishu": -1 }).exec(function(err, docs) { console.log(docs); if(docs.length = 0){ qishu = docs[0].qishu; } }); } }); setInterval(() => { if (countOfYulechang >= 6) { qishu=qishu+1; let shengliguojia= Math.floor( Math.random()*3 )+1; mongoose.connect(db_str,{ useNewUrlParser: true }, function(err) { if (!err) { temp.insertMany([{ qishu: qishu, shengliguojia:shengliguojia }], function(err, docs) { console.log(docs); }); } }); countOfYulechang = 0 } else { countOfYulechang = countOfYulechang + 1 } }, 1000)
下面是修改之前的
mongoose.connect(db_str, { useNewUrlParser: true }, function(err) { if (!err) { var schema = new mongoose.Schema({ qishu: Number, shengliguojia: Number }); var temp = mongoose.model('yulechang', schema, 'yulechang'); temp.find().sort({ "qishu": -1 }).exec(function(err, docs) { console.log(docs); if(docs.length = 0){ qishu = docs[0].qishu; } }); } }); setInterval(() => { if (countOfYulechang >= 6) { qishu=qishu+1; let shengliguojia= Math.floor( Math.random()*3 )+1; mongoose.connect(db_str,{ useNewUrlParser: true }, function(err) { if (!err) { var schema = new mongoose.Schema({ qishu: Number, shengliguojia: Number }); var temp = mongoose.model('yulechang', schema, 'yulechang'); temp.insertMany([{ qishu: qishu, shengliguojia:shengliguojia }], function(err, docs) { console.log(docs); }); } }); countOfYulechang = 0 } else { countOfYulechang = countOfYulechang + 1 } }, 1000)
相关推荐
mkhhxxttxs 2020-06-14
80500495 2020-06-14
80530895 2020-02-23
86211943 2019-12-20
lightlanguage 2019-12-16
80530895 2020-07-05
86211943 2020-03-01
80500495 2020-01-29
86211943 2020-01-24
lovecodeblog 2020-01-24
87261046 2019-12-23
MYRENZHIBO 2019-08-28
85234656 2018-09-03
fudirong 2012-04-12
80500495 2019-07-01
mjperhuo 2019-06-30