用angularJS和输入类型=“”更新mongodb中的日期;日期”;

updating dates in mongodb with angularJS and input type="date"

本文关键字:日期 更新 mongodb angularJS 输入 类型      更新时间:2023-09-26

我有一个MongoDb学生集合。一个学生可以有许多课程(opl),其中包含开始日期和结束日期。我使用Mongoose在server.js:中创建了一个REST接口

//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"})

要查询学生,我在型号上使用此路线:

/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);
        }
    })

})

我的问题是如何使用Angular和HTML5输入type="date",它需要一个date()对象来显示起始数据einddatum日期?我使用这样的Angular服务来创建$scope:

.factory('studentService',['$resource', function($resource){
        var urlBase = "api/studenten";
        var studentResource = $resource(
            urlBase + '/:_id',
            {_id: '@_id'},
            {update: {method:'PUT'}
            }
        )
        return studentResource;
    }])

然后在学生编辑控制器中,我用服务填充$范围:

 .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');
            }

//更多的方法,一些东西遗漏了

如何使用$scope.student.opl.startdatum日期字符串(ISODates)创建要在中使用的日期对象

<input type="date" class="form-control" ng-model="opl.startdatum "  />

这样它的值就是一个ISO字符串,我可以用它来更新课程日期。我一直觉得自己很傻,但没有人展示如何将Mongoose ISOString转换为Date对象。。。所有的例子都是从他们自己创建的一个新的Date()对象开始的。$scope.student.opl不允许我将日期更改为日期对象。我似乎无法将字段添加到。筛选器不起作用。事实上,下面的这个显示日期是正确的,但它会抛出大量错误,更新值为空:

<input type="text" class="form-control" ng-model="opl.einddatum" value="{{opl.einddatum | date:'yyyy-MM-dd'}}" />

使用类似的方法

<input type="date" class="form-control" ng-model="makeDate(opl.startdatum) "  />

也不起作用。我看了angular ui bootstrap,但这里是一样的:你需要一个Date对象。

我可能错过了一个关键的战略点,但我很感激能给我一些帮助,告诉我我的错误,谢谢,

一月

您可以通过以下格式将ISO字符串转换为日期对象,

var date=新日期('2015-11-13T06:16:11.399Z')

在你的情况下,

$scope.student.opl.startdatum=新日期($scope.tudent.opl.startdatum);