我有一段从excel文件中获取团队日程的代码,之后我想做的就是在MongoDB中将这些班次添加到用户的班次中。现在,它将它们添加到一个对象中,并从该对象开始,我尝试将其添加到MongoDB中的用户的shifts字段中,我还尝试在不使用中间对象的情况下直接添加它。但是什么也没发生。
我在任何地方都没有得到错误。我注意到的唯一一件事是,当我添加用户代码时,console.log似乎不再运行。不确定这里出了什么问题。
const ExcelJS = require("exceljs");
const moment = require("moment");
const User = require("../../models/User");
let date = moment("01.06.2020", "DD-MM-YYYY");
const shifts = {
};
times = {
"9-18": {
start: moment("9:00:00", "HH:mm:ss").format("hh:mm A"),
end: moment("18:00:00", "HH:mm:ss").format("hh:mm A"),
},
"12-21": {
start: moment("12:00:00", "HH:mm:ss").format("hh:mm A"),
end: moment("21:00:00", "HH:mm:ss").format("hh:mm A"),
},
"10-14": {
start: moment("10:00:00", "HH:mm:ss").format("hh:mm A"),
end: moment("14:00:00", "HH:mm:ss").format("hh:mm A"),
},
"9-16": {
start: moment("9:00:00", "HH:mm:ss").format("hh:mm A"),
end: moment("116:00:00", "HH:mm:ss").format("hh:mm A"),
},
"15-21": {
start: moment("15:00:00", "HH:mm:ss").format("hh:mm A"),
end: moment("21:00:00", "HH:mm:ss").format("hh:mm A"),
},
"21-1": {
start: moment("21:00:00", "HH:mm:ss").format("hh:mm A"),
end: moment("1:00:00", "HH:mm:ss").format("hh:mm A"),
},
"21-6": {
start: moment("21:00:00", "HH:mm:ss").format("hh:mm A"),
end: moment("6:00:00", "HH:mm:ss").format("hh:mm A"),
},
"00-9": {
start: moment("00:00:00", "HH:mm:ss").format("hh:mm A"),
end: moment("9:00:00", "HH:mm:ss").format("hh:mm A"),
},
DO: { start: "DO", end: "DO" },
CO: { start: "CO", end: "CO" },
};
const readxl = async () => {
const workbook = new ExcelJS.Workbook();
const file = await workbook.xlsx.readFile("../../June.xlsx");
const worksheet = file.getWorksheet("June");
const user = await User.findById("5ee8c995f68af9783014b3f0");
let cell = 5;
let row = 9;
let name = 4;
while (true) {
let rows = worksheet.getRow(row);
if (row === 35) {
break;
} else if (row === 19) {
row = 28;
} else if (row === 31) {
row = 33;
} else if (cell === 35) {
date.subtract(30, "day");
row += 1;
console.log(row);
cell = 5;
}
while (true) {
if (cell === 35) {
break;
}
if (!shifts[rows.getCell(name).value]) {
shifts[rows.getCell(name).value] = [];
}
if (!shifts[rows.getCell(name).value][date]) {
shifts[rows.getCell(name).value][date] = [];
}
shifts[rows.getCell(name).value][date].push({
start: times[rows.getCell(cell).value].start,
end: times[rows.getCell(cell).value].end,
});
// shifts[rows.getCell(name).value].push({
// [date]: {
// start: times[rows.getCell(cell).value].start,
// end: times[rows.getCell(cell).value].end,
// },
// });
cell++;
date.add(1, "days");
}
}
user.shifts.push(shifts["user1"]);
user.save();
console.log(shifts);
};
readxl();**编辑:我已经能够修复它,部分使用了这段代码。我现在的问题是它复制了每个用户的日期/班次。对象本身没有副本,但在mongoDB端有副本。
Edit2:实际上,只有列表中的第一个用户会得到重复的条目,奇怪的是...**
while (true) {
let rows = worksheet.getRow(row);
if (row === 35) {
break;
} else if (row === 19) {
row = 28;
} else if (row === 31) {
row = 33;
} else if (cell === 35) {
date.subtract(30, "day");
row += 1;
console.log(row);
cell = 5;
}
while (true) {
if (cell === 35) {
break;
}
shifts[rows.getCell(name).value].push({
[date]: {
start: times[rows.getCell(cell).value].start,
end: times[rows.getCell(cell).value].end,
},
});
cell++;
date.add(1, "days");
}
}
console.log(shifts);
const newSchedule = new Schedule({
shifts: shifts,
});
newSchedule.save().then((schedule) => {
res.send(schedule);
});发布于 2020-06-23 16:59:53
修改用户失败,因为findById返回promise
const user = await User.findById("5ee8c995f68af9783014b3f0");
user.exec((err,user)=>{//....})或者将您的代码放在回调函数中
const user = await User.findById({"5ee8c995f68af9783014b3f0"},(err,user)=>{
// your code goes here
user.save()
});https://stackoverflow.com/questions/62530520
复制相似问题