2017-08-24 41 views
0

我有一個問題,我用一個get和post函數來檢索並保存一些url在我的數據庫中,但現在我想更新一個變量count,應該rapresent對每個視頻都進行投票,之後我可以禁用用戶點擊投票的按鈕。所以我有一些麻煩,使更新功能,或者我應該使用另一篇文章?但如果是這樣,我可能創建另一個元素在我的數據庫或不?Vue更新貓鼬數據庫中的數據並使用axios調用

所以這裏有我的視頻和現在我設置視頻的ID使用CSS看不見,到

   <p class="invisible" id="idVideo"> {{item._id}} </p> 

       <iframe class="partecipant" v-bind:src="item.video.url"> </iframe> 

       <p id="voti" > {{item.voti.count}} </p> 

       <input type="button" id="buttonVoti" v-on:click="addVoto"> 

所以在這裏,當測試,並試圖找到與特定ID的視頻,並進行更新用戶點擊與ID按鈕= buttonVoti v型上點擊呼叫功能addVoto

methods: { 
... 
//ALL THE OTHERS METHODS 
... 
... 
... 
//AND THEN THE ADDVOTO FUNCTION 
addVoto : function() { 
          var self = this; 
          //self.videos[1].voti.count++ 
          //console.log(self.videos._id); 
          var i = document.getElementById("idVideo"); 
          var idVid =i.innerHTML; 
          console.log(idVid); 

所以在這裏我可以改變的變量數,使用自....算++,但我必須存儲並檢索再次與新計數相同的視頻更新。

這裏有我的模型,邏輯訪問的次數應該是一個

var mongoose = require('mongoose'); 
var Schema = mongoose.Schema; 

var videoSchema = new Schema({ 

    video : { 
     id : String, 
     url : String, 
     idchallenge : String 
    }, 

    voti : { 
     count : {} 
    } 
    }); 


module.exports = mongoose.model('Video', videoSchema); 

是讓我有一種稱爲負載視頻的方法,當用戶點擊一個名爲loadVideo

按鈕時啓動
    loadVideo : function(){ 


         var linkYoutube = this.text; 
         console.log(linkYoutube); 

         //POST 
         axios.post('/video',{ 

          method: 'post', 
          video: { 
           id: '1', 
           url: linkYoutube 
          }, 
          voti: { 
           count: 0 
          } 

         }); 

,這是我的get函數,

     getVideo: function() { 
         var self = this; 

         // Make a request for a user with a given ID 
         axios.get('/video') 
          .then(function (response) { 

           self.videos = response.data; 
           console.log(self.videos); 
+0

我們可以看到向您的服務器發出請求的功能嗎?它應該是你所指的「POST」。我們是否也可以看到您用來獲取所有視頻和他們的投票的Mongoose功能?我認爲它是你所指的GET方法。 –

+0

好吧我編輯了這個問題,這些是我的get和post方法。 –

回答

0

當你做你GET請求您的視頻路線,在您的路線邏輯中,您應該可以使用Mongoose count。下面是這條路線可能是什麼樣子:

var Router = require('express').Router(); 
var mongoose = require('mongoose'); 
var Video = mongoose.model('Video'); 

Router.get('/videos', function(req, res) { 
    var response = {}; 

    Video.find({}, function(queryErr, videos) { 
     if (!queryErr) { 
      response.videos = videos; 

      Video.count({}, function(countErr, count) { 
       if (!countErr) { 
        response.count = count; 
        res.status(200).send(response); 
       } else { 
        res.status(500).send(countErr); 
       } 
      }); 
     } else { 
      res.status(500).send(queryErr); 
     } 
    }); 
}); 

module.exports = Router; 

這是一個關於Stack Overflow貓鼬計數問題。