I am trying to save the attendance of users of their sign in for two type of trip i.e. "up" and "down" and then saving it in the selected route which is selected by user. I want to make the attendance object something like:
attendance = {
up: {
'Wed Mar 15 2017' : [1,2,3.....],
'Wed Mar 16 2017' : [1, 2, 5 ,6 ....]
}
down : {
'Wed Mar 15 2017' : [1,2,3.....],
'Wed Mar 16 2017' : [1, 2, 5 ,6 ....]
}
}
Since for the first time attendance object is not present in the database, I am creating it.
app.get('/attendance', function (req, res) {
var routeId = req.user.place_id;
var userId = req.user._id;
var type = req.query.type;
route.findById(routeId, function (err, selected_route) {
if (err) throw err;
if (selected_route !== null) {
var date = formatDate();
// appending the user attendance to his/her selected route
if (selected_route.attendance !== undefined) {
if (selected_route.attendance[type] !== undefined) {
if (selected_route.attendance[type][date] !== undefined) {
selected_route.attendance[type][date].push(userId);
}
else {
// if(selected_route.attendance.type)
selected_route.attendance[type][date] = [];
selected_route.attendance[type][date].push(userId);
}
}
else {
selected_route.attendance[type] = {};
selected_route.attendance[type][date] = [];
selected_route.attendance[type][date].push(userId);
}
}
else {
selected_route.attendance = [];
selected_route.attendance[type] = {};
selected_route.attendance[type][date] = [];
selected_route.attendance[type][date].push(userId);
}
console.log(type);
// prints the data, but console.log(selected_route.attendance) still prints []
console.log(selected_route.attendance[type]);
// saving the update route info
selected_route.save(function (err) {
if (err) throw err;
res.send("Success");
});
}
else {
res.send("place error");
}
});
});
After saving it, only attendance object appears in database without any field. If anyone can please tell me where am I going wrong? and if there is any other more efficient method to do so. Thanks.
via warl0ck
No comments:
Post a Comment