【问题标题】:Vue @change should update databaseVue @change 应该更新数据库
【发布时间】:2021-03-24 07:16:22
【问题描述】:

我正在创建一个 todolist 并尝试让我的复选框更改 vue 和我的数据库中的属性。如果选中复选框,则复选框应将活动状态更改为 true,如果设置为未选中,则更改为 false。

Vue HTML the v-model change the "active" property. @change should call the function

 <label> active task:  </label> <input type="checkbox"  
     @change="updateActive(todo.id)" v-model="todo.active"> 

The function. If the checkbox is changed from check to uncheck, change the active status to false, vice versa. The error says that e is undefined

    updateActive: function (e){
    if(e.target.checked){

        let url = "/update-task1?active=" + true + "&id=" + id
        fetch(url, {
            method: "PUT",

        })
            .catch(error => {
                console.error('error', error)
            }),
            this.fetchData();

    } else {

        let url = "/update-task1?active=" + false + "&id=" + id
        fetch(url, {
            method: "PUT",

        })
            .catch(error => {
                console.error('error', error)
            }),
            this.fetchData();

    }
},

Express to update the task

app.put('/update-task', function (req, res) {
let collection = 'tasks'
MongoClient.connect(url,
    { useUnifiedTopology: true },
    function (err, db) {
        if (err) throw err;

        let completed = req.query.completed;
        let query = parseInt(req.query.id);
        let completedNewValue = { $set: { 'completed': completed } };
        let updateItem = { 'id': query };
        let databaseobject = db.db("agiledb");

        databaseobject.collection(collection).updateOne(updateItem, completedNewValue),
            function (err, res) {
                if (err) throw err;
                console.log("active");
                db.close();
            };
    });
    });

Property being created in addTodo

    methods: {
    addTodo: function ({target}) {
        if(this.newTodo.trim().length == 0){
            return
        } else {
            console.log("wack")
            this.todos.push({
                id: Date.now(),
                title: this.newTodo,
                created: true,
                active: false,
                completed: false,
                editing: false,
                assigned: this.newAssigned,
            });
            axios.post('/todo', {
                id: Date.now(),
                title: this.newTodo,
                created: true,
                active: false,
                completed: false,
                editing: false,
                assigned: this.newAssigned,
            })

感谢您的帮助

【问题讨论】:

    标签: express vue.js checkbox


    【解决方案1】:

    您要么使用传递的todo.id,要么使用todo.id 之外的事件本身:

    @change="updateActive($event, todo.id)"
    
    updateActive: function (e, todoId){
    ...
    }
    

    【讨论】:

    • 您通过了todo.id,但在updateActive 中您期望的是事件对象。这意味着您应该通过$event 而不是todo.id 或者如果您在updateActive 中都需要两者,则将它们都通过
    • 好的。现在我有:updateActive:函数(e,todo.id)。但我在 todo.id 上遇到错误。当它试图更改数据时,它说:“id is not defined”所以​​我猜它不能再获取任务了。
    • 你能用所有这些更改更新你帖子中的代码吗?
    • 我认为现在的问题是它无法再找到 id 值 updateActive: function (e){ if(e.target.checked){ let url = "/update-task1? active=" + true + "&id=" + id
    • 您没有将updateActive 定义更改为updateActive: function (e, todoId){。这样您就可以将todoId 用作id
    猜你喜欢
    • 2020-08-21
    • 2019-02-18
    • 1970-01-01
    • 2020-08-19
    • 1970-01-01
    • 2020-05-04
    • 2023-03-05
    • 2014-08-09
    • 2021-11-06
    相关资源
    最近更新 更多