1


I made an app with Mean framework (Angular, Express, MongoDB). User can post anything. Now I display author, title etc... but I would like to display the post date.
I tried something, and I succed to display the current date, but if I'll refresh tomorrow, the date of posts will change to tomorrow date.

This is my Post schema :

var PostSchema = new mongoose.Schema({
    title : String,
    link : String, 
    author : String,
    tagg : String,
    date: { type: Date, default: Date.now }, 
    upvotes: {type: Number, default :0},
    comments: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Comment' }],
    userliked: {type: mongoose.Schema.Types.ObjectId, ref: 'User' }
});

In my MainCtrl :

$scope.addPost = function(){

        if(!$scope.title || $scope.title === ''){
            return;
        }
        posts.create({
            title : $scope.title, 
            link : $scope.link,
            tagg: $scope.tagg,
            date : $scope.date,
        });

        $scope.title = '';
        $scope.link = '';
        $scope.tagg = '';
        $scope.date = '';
    };

And in the HTML :

<div class="post-line" ng-repeat="post in posts | orderBy: ' -upvotes' | filter: expression" style=" padding-bottom:10px; margin-bottom:30px; background-color:rgba(0,0,0,0.02); padding:15px; box-shadow:0 2px 4px rgba(0,0,0,0.25);">
<div style="font-size:20px; margin-left:10px;display:inline;">
    <span ng-show="post.link" id="target" style="display:inline;">
        <a href="{{post.link}}" target="_blank"> {{post.title}}</a>
    </span>
    <span ng-hide="post.link" class="post-title">
        {{post.title}}
    </span>
    <span ng-show="post.tagg" style="font-size:14px;color:rgba(0,0,0,0.5);display:block;font-style:italic;">
        {{post.tagg}}
    </span>
    <span ng-show="post.author" style="font-size:14px;">
        posted by 
        <span style="font-weight:bold;">{{post.author}}</span> 
            le
        <span style="font-size:13px;">{{post.date | date:'dd/MM/yyyy'}}</span>
    </span>
</div> 
</div>


And of course, it doesn't display anything. Thanks by advance for your help !

EDIT :
If I make a console.log of my post object, I see the post date, but all of the posts have the same date and same hour..

Nitneq
  • 651
  • 10
  • 26
  • Can you post the `ng-repeat` code (i assume there is an `ng-repeat`)? Does it display anything if you remove the filter? – Gian Marco Toso May 04 '16 at 08:44
  • We will need more code from the server side. Have you checked the database (either by the mongo shell or any gui like robomongo) that the posts are actually saved with the date of the post in them? My guess is that you either havent saved them, havent saved them with the date set or don't retrieve the date when reading posts. In any case, I think its going wrong server side. – RobbyD May 04 '16 at 08:52
  • @juandemarco It's done. And nope it doesn't display anything without the filter too. RobbyD I made a console.log to display the object Post, and you're right, there is no Date saved inside.. – Nitneq May 04 '16 at 09:00

2 Answers2

1

Mongo needs a JavaScript Date object to store a date. So, you want to store the current date (and time) when creating a post, and to do that you need to crate a new Date object without arguments. You can add that object to your create function.

For example:

$scope.addPost = function(){

    if(!$scope.title || $scope.title === ''){
        return;
    }

    // Creates a new Date() object with current day and time
    // For example: "Wed May 04 2016 15:41:13 GMT+0200 (W. Europe Standard Time)"
    var postDate = new Date();

    posts.create({
        title : $scope.title, 
        link : $scope.link,
        tagg: $scope.tagg,
        date : postDate,
    });

    $scope.title = '';
    $scope.link = '';
    $scope.tagg = '';
    $scope.date = '';
};
Lodybo
  • 467
  • 5
  • 13
  • Sorry maybe I was not clear. User doesn't have to enter date. If he posts the 05/04 at 11h10, it's this date who has to be displaying. – Nitneq May 04 '16 at 09:03
  • You mean, that the user doesn't enter a date, but that the current date + time gets sent along with `post.create()`? – Lodybo May 04 '16 at 12:37
  • Yes effectively my code could let you think that. I wasn't sure about what I wrote, it was after several test and some search. Date had to be added automatically. – Nitneq May 04 '16 at 13:22
  • 1
    I think that I tried something too complicated. Finally I found a simple solution. I keep my schema in Post Schema, delete my function in my controller, and just add this : {{post.date | date:'dd/MM/yyyy à HH:mm'}} and it works .. But thanks you so much for your help ! – Nitneq May 04 '16 at 14:10
  • Glad you found a solution! – Lodybo May 06 '16 at 11:46
1

So finally it's more simple than I thought..
In the Post Schema :

date: { type: Date, default: Date.now } 

In the HTML :

<span >{{post.date | date:'dd/MM/yyyy à HH:mm'}}</span>

And it works. I hope it could help people with the same issue. Thanks to everybody who help me.

Nitneq
  • 651
  • 10
  • 26