Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
193 views
in Technique[技术] by (71.8m points)

javascript - How can to change isActive boolean property from false to true?

This is my enable 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.")
    }
})

This is my controller

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

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

This is my routes

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

please help me point out what's wrong with my code, my console doesn't return any error, but the value isActive of my data doesn't update either.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think findOneAndUpdate does not return an error in .then and you should catch it with .catch

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...