如何将 isActive 布尔属性从 false 更改为 true?

时间:2021-01-05 11:14:43

标签: javascript node.js json api

这是我启用的 course.js

let params = new URLSearchParams(window.location.search)

let courseId = params.get('courseId')

let token = localStorage.getItem('token')

fetch(`http://localhost:3000/api/courses/${courseId}`, {
    method: "PUT",
     headers: {
    'Content-Type': 'application/json',
    'Authorization': `Bearer ${token}`
    },
    body: JSON.stringify({
        isActive: true
    })
})
.then(res => res.json())
.then(data => {
    if(data === true){
        window.location.replace('./courses.html')
    }else{
        alert("Something went wrong.")
    }
})

这是我的控制器

module.exports.unArchive = (params) => {
    let update = {
        isActive: true
    }

    return Course.findOneAndUpdate(params.courseId, update)
    .then((course, err) => {
        return (err) ? false : true
    })
}

这是我的路线

router.put('/:courseId', auth.verify, (req, res) => {
    let courseId = req.params.courseId
    CourseController.unArchive({courseId})
   .then(resultFromUnArchive => res
  .send(resultFromUnArchive))
})

请帮我指出我的代码有什么问题, 我的控制台没有返回任何错误, 但我的数据的值 isActive 也没有更新。

1 个答案:

答案 0 :(得分:0)

我认为 findOneAndUpdate 不会在 .then 中返回错误,您应该使用 .catch 捕获它

module.exports.unArchive = (params) => {
  let update = {
    isActive: true,
  };

  return Course.findOneAndUpdate(params.courseId, update)
    .then((course) => true)
    .catch((err) => false);
};