Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I check If a value of a Boolean Variable is false?

I don’t know If I’m checking for the value of the boolean correctly

what this code does: the user creates a note for himself, his ID is on the note and it needs to belong to a category name that has to be in the category schema ( where my error happens )

exports.postAddNote = (req, res) => {
  const errors = validationResult(req);
  if (!errors.isEmpty()) {
    const error = new Error("validation failed, entered data is incorrect");
    throw error;
  }
  const content = req.body.content;
  const tags = req.body.tags;
  const categoryName = req.body.categoryName;
  let creator;
  const note = new Note({
    content: content,
    categoryName: categoryName, //  work
    tags: tags,
    creator: req.userId,
  });
  Category.find()
    .select("-_id")
    .select("-__v")
    .select("-notesId")
    .then((categories) => {
      console.log(categories); //stripping everything but names off categories
      const CategoryExists = categories.some(
        (category) => category.name === categoryName
      );
      console.log(CategoryExists); // ~~~~~~~~~~ this logs correctly
      if (CategoryExists === -0) { // ~~~~~~~~~~ what i want: if the value is false
        return res.json({ Error: "The category you entered does not exist" });
      } 
      note // ~~~~~~~~~~ the code stops here :/ it doesn't save the note
        .save()
        .then((note) => {
          console.log("saved note");
          User.findById(req.userId);
        })
        .then((user) => {
          creator = user;
          user.notes.push(note);

          return user.save();
        })
        .then((result) => {
          res.status(201).json({
            info: {
              dateCreated: new Date().toISOString(),
              status: "Note Created Successfully",
              creator: { _id: creator._id, email: creator.email },
            },
          });
        })
        .catch((err) => {
          if (!err.statusCode) {
            err.statusCode = 500;
          }
        });
    })
    .catch((err) => {
      console.log(err);
      next();
    });
};

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

if (CategoryExists === -0)

should be

if (CategoryExists === false)

or just

if (!CategoryExists)

i believe. did you try that? not sure why you are using -0. the return value for some() is either going to be true or false.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading