【问题标题】:Vue update data within mongoose database and using axios callVue更新猫鼬数据库中的数据并使用axios调用
【发布时间】:2018-02-02 20:13:53
【问题描述】:

我有一个问题,我使用 get 和 post 函数在我的数据库中检索和保存一些 url,但现在我想更新一个变量计数,它应该代表每个视频获得的投票,然后我应该能够禁用用户点击投票的按钮。所以我在制作更新功能时遇到了一些麻烦,或者我应该使用另一个帖子?但如果是这样,我可能会在我的数据库中创建另一个元素吗?

所以这里有我的视频,现在我使用 css 将视频的 id 设置为不可见,以对其进行测试并尝试找到具有该特定 id 的视频并进行更新

                <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= buttonVoti 的按钮时,v-on click 调用 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);

所以在这里我可以使用 self....count++ 更改变量计数,但我必须存储并再次检索更新了新计数的相同视频。

这里有我的模型,所以访问计数的逻辑应该是这个

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

【问题讨论】:

  • 我们能看到向您的服务器发出请求的函数吗?它应该是您所指的POST。我们还能看到您用来获取所有视频及其投票的 Mongoose 功能吗?我认为这是您所指的GET 方法。
  • 好吧,我编辑了问题,这些是我的 get 和 post 方法。

标签: javascript node.js mongodb vue.js axios


【解决方案1】:

当你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 上的 Mongoose 计数的问题。

【讨论】:

    猜你喜欢
    • 2021-11-16
    • 1970-01-01
    • 2015-01-07
    • 1970-01-01
    • 2019-08-31
    • 2019-11-25
    • 1970-01-01
    • 2018-12-03
    • 1970-01-01
    相关资源
    最近更新 更多