Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 选中选中body时如何更新MongoDB中的数据_Javascript_Node.js_Mongodb_Backend - Fatal编程技术网

Javascript 选中选中body时如何更新MongoDB中的数据

Javascript 选中选中body时如何更新MongoDB中的数据,javascript,node.js,mongodb,backend,Javascript,Node.js,Mongodb,Backend,当我选中复选框而不提交任何表单时,如何更新MongoDB中的数据 我的模式 const userSchema = new mongoose.Schema({ name: { type: String, trim: true, }, todos: [ { task: { type: String, trim: true,

当我选中复选框而不提交任何表单时,如何更新MongoDB中的数据

我的模式

const userSchema = new mongoose.Schema({
    name: {
        type: String,
        trim: true,
    },
    todos: [
        {
            task: {
                type: String,
                trim: true,
                required: 'Please Enter your Task',
            },
            dueDate: {
                type: Date,
                default: new Date(+new Date() + 3 * 24 * 60 * 60 * 1000),
            },
            dueTime: String,
            done: {
                type: Boolean,
                default: false,
            },
        },
    ],
});

我想更新
todos
数组中的
done
元素

我试着这么做

主客户端JavaScript

$(document).ready(function () {
    $('.todo--checkbox').change(function () {
        let isChecked;
        if (this.checked) {
            isChecked = true;
            $.ajax({
                url: '/todo/' + this.value,
                type: 'PUT',
                data: { done: true },
            });
        } else {
            isChecked = false;
            $.ajax({
                url: '/todo/' + this.value,
                type: 'PUT',
                data: { done: false },
            });
        }
    });
});
在前端,我将复选框的值设置为对象的
\u id

/routes/index.js我正在处理我的路线

router.put('/todo/:id', todoControllers.checkStatus);
最后,我在我的todoCOntroller.js中处理该控制器

exports.checkStatus = async (req, res) => {
    try {
        const user = await User.aggregate([
            { $unwind: '$todos' },
            { $match: { 'todos._id': req.params.id } },
        ]);

        // res.json(user);
        console.log(user);
    } catch (err) {
        console.log('error: ', err);
    }
};
但我的控制台中没有任何
用户


请告诉我哪里错了。

您不需要使用聚合。您可以使用
$elemMatch

const user = await User.find({
    todos: { $elemMatch: { _id: req.params.id } },
});

有关更多信息,请阅读

哪一步有问题?
$unwind
是否返回某些内容?您的整个
.change
函数可以编写为
.change(函数(){$.ajax({url:…,type:…,数据:{done:this.checked})
。当前,您正在创建一个新变量,该变量将根据另一个变量是真是假而变为真或假,然后检查该变量是真还是假,并复制代码,一次为真,一次为假。整个过杀逻辑可以简化为
数据:{done:This.checked}
.Yes
$unwid
工作正常,如果我使用
const user=wait user.aggregate([{$unwind:'$todos'},{$match:{'todos.task':'task4'}},]);
它还返回正确的数据。问题是当我使用
{$match:{'todos.\u id':req.params.id}时,
好的,我现在正在猜测,但可能是
todos.task.\u id:req.params.id
todos.task.\u id:req.params.id
不起作用