/** * @desc 同步数据 */router.post("/user/copy", (req, res) = { const query = User.find({}); let pageSize = 2; //查询数量 let pageCount = 1; //查询页码 let insertFlag = true; //是否添加数据 let setTime = setInterval(() = {
/** * @desc 同步数据 */ router.post("/user/copy", (req, res) => { const query = User.find({}); let pageSize = 2; //查询数量 let pageCount = 1; //查询页码 let insertFlag = true; //是否添加数据 let setTime = setInterval(() => { let startPage = (pageCount - 1) * pageSize; //开始条数 query.limit(pageSize); //设置查询条数 query.skip(startPage); //开始条数 console.log("pageSize", pageSize, "startPage", startPage, "pageCount", pageCount) pageCount += 1; query.exec((err, data) => { if (err) throw err; if (data.length < pageSize) { console.log(‘close‘); clearInterval(setTime); } for (var i = 0; i < data.length; i++) { const userData = new UserData({ _id: data[i]._id, name: data[i].name, password: data[i].password, type: data[i].type, avatar: data[i].avatar, date: data[i].date }); //去除重复数据 UserData.findOne({ _id: objectId(data[i]._id) }, function (err, doc) { if (err) throw err; if (doc != null) { insertFlag = false; } } ); if (insertFlag) { userData .save() .then(userData => res.json(userData)) .catch(err => console.log(err)); } } }); }, 1000) });