Node.js 猫鼬不';除非重新启动nodejs,否则无法获取更新的数据
我有一个叫特工的猫鼬收藏。我可以使用mongodb控制台查看更新的数据。但无法使用$.ajax或$.get获取更新的数据 我对野猫和猫鼬还不熟悉。我更新的数据在mongodb数据库上得到更新,但即使在刷新之后也不会在客户机上得到更新 将更新的集合发送到客户端的路由Node.js 猫鼬不';除非重新启动nodejs,否则无法获取更新的数据,node.js,mongodb,express,mongoose,Node.js,Mongodb,Express,Mongoose,我有一个叫特工的猫鼬收藏。我可以使用mongodb控制台查看更新的数据。但无法使用$.ajax或$.get获取更新的数据 我对野猫和猫鼬还不熟悉。我更新的数据在mongodb数据库上得到更新,但即使在刷新之后也不会在客户机上得到更新 将更新的集合发送到客户端的路由 router.get('/:function', function(req, res) { if (req.params.function === 'getWeeklyTarget') { helper.getWeekly
router.get('/:function', function(req, res) {
if (req.params.function === 'getWeeklyTarget') {
helper.getWeeklyTarget().then(values => {
res.send(values);
})
}
})
getWeeklyTarget函数
function getWeeklyTarget() {
return new Promise((resolve, reject) => {
// Find Current Week
let this_month = `${moment().format('MMMM')} ${moment().format('YYYY')}`;
let this_week;
let team_targets = [];
Month.find({name: this_month})
.then(foundMonth => {
foundMonth.forEach(month => {
if (month.name === this_month) {
this_week = month.current_week;
}
findTeams.then(foundTeams => {
let this_week_target = [];
foundTeams.forEach(team => {
let counter = 0;
let another_counter = 0;
team.members.forEach(agent => {
if (this_week === 'first_week') {
counter += agent.weekly_target.first_week
another_counter += agent.total_sales
}else if (this_week === 'second_week') {
counter += agent.weekly_target.second_week
another_counter += agent.total_sales
}else if (this_week === 'third_week') {
counter += agent.weekly_target.third_week
another_counter += agent.total_sales
} else if (this_week === 'fourth_week') {
counter += agent.weekly_target.fourth_week
another_counter += agent.total_sales
}
})
this_week_target.push({
name: team.name,
target: counter,
achieved: another_counter
})
})
resolve(this_week_target);
})
})
})
})
}
一旦集合被更新,我想将更新后的数据提取到客户机似乎
resolve()
方法被多次调用,因为它在forEach
循环中被调用。尝试在forEach
循环外声明this\u week\u目标,并在forEach循环外调用resolve()
。像这样
router.get('/:function', function(req, res) {
if (req.params.function === 'getWeeklyTarget') {
helper.getWeeklyTarget().then(values => {
res.send(values);
})
}
})
function getWeeklyTarget() {
return new Promise((resolve, reject) => {
// Find Current Week
let this_month = `${moment().format('MMMM')} ${moment().format('YYYY')}`;
let this_week;
let team_targets = [];
Month.find({name: this_month})
.then(foundMonth => {
let this_week_target = [];
foundMonth.forEach(month => {
if (month.name === this_month) {
this_week = month.current_week;
}
findTeams.then(foundTeams => {
foundTeams.forEach(team => {
let counter = 0;
let another_counter = 0;
team.members.forEach(agent => {
if (this_week === 'first_week') {
counter += agent.weekly_target.first_week
another_counter += agent.total_sales
}else if (this_week === 'second_week') {
counter += agent.weekly_target.second_week
another_counter += agent.total_sales
}else if (this_week === 'third_week') {
counter += agent.weekly_target.third_week
another_counter += agent.total_sales
} else if (this_week === 'fourth_week') {
counter += agent.weekly_target.fourth_week
another_counter += agent.total_sales
}
})
this_week_target.push({
name: team.name,
target: counter,
achieved: another_counter
})
})
})
})
resolve(this_week_target);
})
})
}
似乎resolve()
方法被多次调用,因为它是在forEach
循环中调用的。尝试在forEach
循环外声明this\u week\u目标,并在forEach循环外调用resolve()
。像这样
function getWeeklyTarget() {
return new Promise((resolve, reject) => {
// Find Current Week
let this_month = `${moment().format('MMMM')} ${moment().format('YYYY')}`;
let this_week;
let team_targets = [];
Month.find({name: this_month})
.then(foundMonth => {
let this_week_target = [];
foundMonth.forEach(month => {
if (month.name === this_month) {
this_week = month.current_week;
}
findTeams.then(foundTeams => {
foundTeams.forEach(team => {
let counter = 0;
let another_counter = 0;
team.members.forEach(agent => {
if (this_week === 'first_week') {
counter += agent.weekly_target.first_week
another_counter += agent.total_sales
}else if (this_week === 'second_week') {
counter += agent.weekly_target.second_week
another_counter += agent.total_sales
}else if (this_week === 'third_week') {
counter += agent.weekly_target.third_week
another_counter += agent.total_sales
} else if (this_week === 'fourth_week') {
counter += agent.weekly_target.fourth_week
another_counter += agent.total_sales
}
})
this_week_target.push({
name: team.name,
target: counter,
achieved: another_counter
})
})
})
})
resolve(this_week_target);
})
})
}
这个问题仍然存在。Mongoose仍然没有返回更新的数据。问题仍然存在。Mongoose仍然不会返回更新的数据。