我不知道我是否正確檢查布林值
此代碼的作用:用戶為自己創建一個便箋,他的 ID 在便箋上,并且它需要屬于必須在類別架構中的類別名稱(我的錯誤發生的地方)
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();
});
};
uj5u.com熱心網友回復:
if (CategoryExists === -0)
應該
if (CategoryExists === false)
要不就
if (!CategoryExists)
我相信。你試過嗎?不知道你為什么使用-0
. 的回傳值some()
要么是true
要么false
。
uj5u.com熱心網友回復:
嘗試這個:
if (!CategoryExists) {
return res.json({ Error: 'The category you entered does not exist' });
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/520166.html
下一篇:C#將不帶空格的字串添加到陣列中