我有一些对象要保存在数据库中,但每次执行脚本时它们都必须是唯一的。这些对象只会改变应用程序的每个实例的值。问题是当我创建第一个对象时,之后每次执行脚本时也会创建更多副本。我尝试再次使用同一个对象,但是当我执行查询时,我从以前的执行中获取对象,给我错误的值。我只是希望能够将相同的对象重置为其默认值,并且在 ScriptDb 中没有很多不必要的副本。
这是一些代码:
// Get a database instance
var database = getDb();
// Create a new employee instance
var myEmployee = {
element: "currentEmployee",
firstName: "",
lastName: "",
ID: 0,
manager: "",
managerEmail: "",
department: "1 - University Store",
startDate: "",
endDate: "",
regularHours: 0,
vacationHours: 0,
sickHours: 0,
personalHours: 0,
H2Hours: 0,
overtimeHours: 0,
totalHours: 0,
rowLocation: 0,
salaryType: "H",
};
var week1 = {
// Identify the week and for query purposes
element: "Week1",
// User entries
HW: {THUR: 0, FRI: 0, SAT: 0, SUN: 0, MON: 0, TUES: 0, WED: 0},
VH: {THUR: 0, FRI: 0, SAT: 0, SUN: 0, MON: 0, TUES: 0, WED: 0},
SH: {THUR: 0, FRI: 0, SAT: 0, SUN: 0, MON: 0, TUES: 0, WED: 0},
PH: {THUR: 0, FRI: 0, SAT: 0, SUN: 0, MON: 0, TUES: 0, WED: 0},
TH: {THUR: 0, FRI: 0, SAT: 0, SUN: 0, MON: 0, TUES: 0, WED: 0},
// Calculated totals
TotalHW:0, TotalVH: 0, TotalSH: 0, TotalPH: 0, TotalHours: 0,
// Week totals
Vacation: 0, Sick: 0, Personal: 0, H2: 0, OT: 0, Regular: 0,
}
var week2 = {
// Identify the week and for query purposes
element: "Week2",
// User entries
HW: {THUR: 0, FRI: 0, SAT: 0, SUN: 0, MON: 0, TUES: 0, WED: 0},
VH: {THUR: 0, FRI: 0, SAT: 0, SUN: 0, MON: 0, TUES: 0, WED: 0},
SH: {THUR: 0, FRI: 0, SAT: 0, SUN: 0, MON: 0, TUES: 0, WED: 0},
PH: {THUR: 0, FRI: 0, SAT: 0, SUN: 0, MON: 0, TUES: 0, WED: 0},
TH: {THUR: 0, FRI: 0, SAT: 0, SUN: 0, MON: 0, TUES: 0, WED: 0},
// Calculated totals
TotalHW:0, TotalVH: 0, TotalSH: 0, TotalPH: 0, TotalHours: 0,
// Week totals
Vacation: 0, Sick: 0, Personal: 0, H2: 0, OT: 0, Regular: 0,
}
// Save these to the database
database.save(myEmployee);
database.save(week1);
database.save(week2);
然后,我将使用该语句ScriptDb.getMyDb.query({element: element}).next()
来检索对象。包含“currentEmployee”、“Week1”或“Week2”的字符串元素。