简体   繁体   中英

Angular - Mongoose : Can't manipulate dates

I have been searching for a long while for solution, but nothing helped me.

I have an Angular JS app which runs with Mongoose and Express.

I want to store date as object from a simple form. But when I submit my form, dates are stored as String and not as Objects. So I can't do something like :

tache.start.getDate();

Here is my form :

<form  name="formTache" ng-submit="addTache(tache)">
    <div class="form-group row">
        <div class="col-lg-12">
            <input name="name" class="form-control" placeholder="Nom" type="text" ng-model="tache.title"/>
        </div>
    </div>
    <div class="form-group row">
        <div class="col-lg-12">
            <input id="date" name="date" class="form-control" placeholder="Date" ng-model="tache.start" type="date"/>
        </div>
    </div>
    <div class="form-group row">
         <div class="text-right col-lg-12">
            <button type="submit" class="btn btn-default">Ajouter</button>
         </div>
    </div>

Here is my Mongoose Schema :

var restful = require('node-restful');
var mongoose = restful.mongoose;
var Schema = mongoose.Schema,
ObjectId = Schema.ObjectId;
var tachesSchema = new mongoose.Schema({
    title : String,
    start: {type: Date, default: new Date()},
});
var tachesModel = mongoose.model('taches', tachesSchema);

module.exports = restful.model('taches', tachesSchema);

Here is my Controller :

angular.module('personaldashboard.tache', [])
  .controller('TachesCtrl', function($scope, Taches, Progress, toaster) {
    $scope.tache = new Taches();
    var refreshTache = function() {
      $scope.taches = Taches.query(); 
      $scope.tache = ''
    }
    refreshTache();

    $scope.addTache = function(tache) {
      Taches.save(tache,function(tache){
        refreshTache();
      });
    };

    $scope.updateTache = function(tache) {
      tache.$update(function(){
        refreshTache();
      });
    };

    $scope.removeTache = function(tache) {
      tache.$delete(function(){
        refreshTache();
      });
    };

    $scope.editTache = function(id) {
      $scope.tache = Taches.get({ id: id });
    };  

    $scope.deselectTache = function() {
      $scope.tache = ''
    }
    $scope.editTache_Projet = function(tache, projetId) {
      tache.projet = projetId;
      tache.$update(function(){
        refreshTache();
      });
    };
    });

Here is what I get :

{ "_id": "58a99df24975ad0104c692b1", "title": "Test", "start": "2017-02-24T23:00:00.000Z" }

So why do I get a string like "2017-02-24T23:00:00.000Z" for my date instead of an object whereas my Mongoose schema specify start: {type: Date, default: new Date()} ?

Thanks for your help.

EDIT

Thanks to Saurabh Agrawal, I tried to convert the date when submiting in the controller :

$scope.addTache = function(tache) {
  tache.start = new Date(tache.start);
  tache.end = new Date(tache.end);
  Taches.save(tache,function(tache){
    refreshTache();
  });
};

Sadly, this changed nothing :(

I still have a date as string

 "2017-02-20T23:00:00.000Z" 

EDIT

I also tried to add a directive

.directive("formatDate", function(){
  return {
   scope: {ngModel:'='},
    link: function(scope) {
        if (scope.ngModel) {
            scope.ngModel = new Date(scope.ngModel);
        }
    }
  }
})

and call it in my form

<form  name="formTache" ng-submit="addTache(tache)">
    <div class="form-group row">
        <div class="col-lg-12">
            <input name="name" class="form-control" placeholder="Nom" type="text" ng-model="tache.title"/>
        </div>
    </div>
    <div class="form-group row">
        <div class="col-lg-12">
            <input id="date" name="date" class="form-control" placeholder="Date" ng-model="tache.start" type="date" formatDate/>
        </div>
    </div>
    <div class="form-group row">
         <div class="text-right col-lg-12">
            <button type="submit" class="btn btn-default">Ajouter</button>
         </div>
    </div>

But nothing changes.

Any other ideas ?

By searching, I 've found that I was missing the real problem.

My dates are date objects.

When i do this

$scope.test = new Date([2017,2,15]);

<pre>{{test}}</pre>
<pre>{{test.getDate()}}</pre>

I get

"2017-02-14T23:00:00.000Z" and 15

So date displaying like "2017-02-14T23:00:00.000Z" are objects.

But in my case, when i try to do the same with a date whitch is in another object like in this schema :

 var tachesSchema = new mongoose.Schema({
    title : String,
    start: {type: Date, default: new Date()},
    end: {type: Date, default: new Date()},
    comment : String,
    state : Boolean,
    projet : { type: ObjectId, ref: 'Projets' }
});

I get nothing :(

This code :

{{tache.start}}

displays the date like this "2017-02-20T23:00:00.000Z"

but

<pre>{{tache.start.getDate()}}</pre>

displays nothing.

What I missed ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM