I have a MongoDb student collection. A student can have a number of courses (opl) which contains start- and enddates. I use Mongoose to create a REST interface in server.js:
//adres Schema
var adresSchema = new mongoose.Schema({
gemeente: String,
post: Number,
straat: String
})
//opl Schema
var oplSchema = new mongoose.Schema({
oplcode: Number,
geslaagd: Boolean,
startdatum: Date,
einddatum: Date,
})
//Studenten Schema
var studentSchema = new mongoose.Schema({
voornaam: String,
familienaam: String,
email: String,
adres: adresSchema,
foto: String,
ikl: Number,
opl: [oplSchema],
rijksreg: String,
gender: String,
tel: [String]
}, {collection:"studenten"})
to query for a student I use this Route on the Model:
/GET /api/studenten/:id 1 enkele student by id
apiRouter.get('/studenten/:id', function(req, res) {
return studentModel.findById(req.params.id,function(err, student) {
if (!err) {
res.send(student);// 1 student
}
else {
return console.log(err);
}
})
})
My problem is how to use Angular and the HTML5 input type="date" which requires a Date() object to show the startdatum and einddatum dates? I use an Angular service like this to create the $scope:
.factory('studentService',['$resource', function($resource){
var urlBase = "api/studenten";
var studentResource = $resource(
urlBase + '/:_id',
{_id: '@_id'},
{update: {method:'PUT'}
}
)
return studentResource;
}])
Then in the student Edit controller I fill the $scope with the service:
.controller('StudentEditCtrl', ['$scope', '$routeParams', 'studentService', 'opleidingService','$location',
function ($scope, $routeParams, studentService, opleidingService, $location) {
//update student and his courses (opl)
$scope.subtitel = "Update student";
$scope.student = studentService.get({}, {'_id': $routeParams._id});
//save
$scope.save = function (student) {
if ($scope.student._id) {
//update
console.log("updating student " + student.familienaam);
studentService.update({_id: $scope.student._id}, $scope.student);
}
else {
//new student
$scope.student.$save();
}
$location.path('/studenten');
}
//more methods, some things left out
How can I use my $scope.student.opl.startdatum dateStrings (ISODates) to create a Date object to be used in
<input type="date" class="form-control" ng-model="opl.startdatum " />
such that its value is an ISO String and I can use it to update the courses dates. I have been reading myself silly, but nowhere does anyone show how to convert the Mongoose ISOString to a Date object... all examples start with a new Date() object they create themselves. The $scope.student.opl doesn't allow me to change the dates to a Date Object. I don't seem to be able to add a field to it to. Filters don't work. Actually this below displays the date correctly, but it throws up a whole number of errors and the update value is empty:
<input type="text" class="form-control" ng-model="opl.einddatum" value="{{opl.einddatum | date:'yyyy-MM-dd'}}" />
Using a method like
<input type="date" class="form-control" ng-model="makeDate(opl.startdatum) " />
doesn't work either. I looked at angular-ui-bootstrap, but here it is the same: you need a Date object.
I am probably missing a crucial strategic point here, but I would appreciate some help to show me my mistake, thanks,
Jan