Просмотр исходного кода

Fix bug with creating a new comment.
Add time to the comment display.

Lee Morgan 5 лет назад
Родитель
Сommit
54ca9585c1
2 измененных файлов с 21 добавлено и 5 удалено
  1. 8 1
      controller.js
  2. 13 4
      views/writing/index.js

+ 8 - 1
controller.js

@@ -7,7 +7,14 @@ module.exports = {
     response = [Comment]
     */
     getComments: function(req, res){
-        Comment.find({article: req.params.article})
+        Comment.aggregate([
+            {$match: {
+                article: req.params.article
+            }},
+            {$sort: {
+                date: -1
+            }}
+        ])
             .then((comments)=>{
                 return res.json(comments);
             })

+ 13 - 4
views/writing/index.js

@@ -2,6 +2,7 @@ fetch("/writing/comments/touchscreens")
     .then(response => response.json())
     .then((response)=>{
         if(typeof(response) === "string"){
+            console.log(response);
         }else{
             let comments = document.getElementById("comments");
             let template = document.getElementById("comment").content.children[0];
@@ -9,19 +10,22 @@ fetch("/writing/comments/touchscreens")
             let dateOptions = {
                 month: "long",
                 day: "numeric",
-                year: "numeric"
+                year: "numeric",
+                hour: "2-digit",
+                minute: "2-digit"
             };
 
             for(let i = 0; i < response.length; i++){
                 let comment = template.cloneNode(true);
                 comment.children[0].children[0].innerText = response[i].name;
-                comment.children[0].children[1].innerText = new Date(response[i].date).toLocaleDateString("en-US", dateOptions);
+                comment.children[0].children[1].innerText = new Date(response[i].date).toLocaleString("en-US", dateOptions);
                 comment.children[1].innerText = response[i].content;
                 comments.appendChild(comment);
             }
         }
     })
     .catch((err)=>{
+        console.log(err);
     });
 
     createComment = ()=>{
@@ -44,23 +48,28 @@ fetch("/writing/comments/touchscreens")
             .then(response => response.json())
             .then((response)=>{
                 if(typeof(response) === "string"){
+                    console.log(response);
                 }else{
+                    
                     let comments = document.getElementById("comments");
                     let comment = document.getElementById("comment").content.children[0].cloneNode(true);
 
                     let dateOptions = {
                         month: "long",
                         day: "numeric",
-                        year: "long"
+                        year: "numeric",
+                        hour: "2-digit",
+                        minute: "2-digit"
                     };
 
                     comment.children[0].children[0].innerText = response.name;
-                    comment.children[0].children[1].innerText = new Date(response.date).toLocaleDateString("en-US", dateOptions);
+                    comment.children[0].children[1].innerText = new Date(response.date).toLocaleString("en-US", dateOptions);
                     comment.children[1].innerText = response.content;
                     comments.insertBefore(comment, comments.children[1]);
                 }
             })
             .catch((err)=>{
+                console.log(err);
             });
     }