Я создаю веб-сайт, на котором вы можете войти и создать задачи, а затем проверить их. Я работаю с mongoose, и задачи сохраняются в массиве типа Task в модели User. Каждая задача имеет значение «выполнено», которое имеет значение «истина» или «ложь» (не логическое значение), когда пользователь проверяет задачу как выполненную, запрос ajax Put отправляется на сервер и должен изменить значение «выполнено»,Я не могу изменить готовое значение. Я думаю, что проблема заключается в доступе к объекту «done» в объекте «task» в массиве Task в модели User.
Я пробовал все виды синтаксисов, показанных на официальном веб-сайте mongoose, но ничего не кажетсяработать.
//send a task(a string with the actual task) and the new done situation to the server
function putDone(task,done){
$.ajax({
type: 'Put',
data:{
task,
done,
},
url: '/tasks/tasks',
})
}
//find the task from all the user's tasks and replaces the done situation
router.put("/tasks",(req,res) => {
for(var i = 0; i < req.user.tasks.length; i++){
if(req.user.tasks[i].task == req.body.task){
User.findOneAndUpdate(
{'username': req.user.username},
{'$set': { 'req.user.tasks[i-1].done': req.body.done }},
{'new': true},
function(err,updatedUser){
if(err){
console.log(err);
}else{
console.log(req.body.task + ", " + req.body.done);
console.log(updatedUser.tasks[i-1]);
}
}
);
}
}
});
модели, заданные в ответах:
//task schema
//var Task = mongoose.Schema.Types.Task;
let userSchema = mongoose.Schema({
email:{
type: String,
required:true,
},
username:{
type: String,
required: true,
},
password:{
type: String,
required: true,
minLength: 8,
maxLength: 20,
},
archive:{
type: Array,
required: false,
},
tasks:{
type: Array,
required: false,
}
});
let User = module.exports = mongoose.model('User', userSchema);
//task schema
let taskSchema = mongoose.Schema({
task:{
type: String,
required: true,
minlength: 2,
},
done:{
type: Boolean,
default: false,
required: true,
}
});
let Task = module.exports = mongoose.model('Task', taskSchema);//find by first param the collection, can add third param of actual collection name