I am wanting to give a custom message when a GET request is called when an array has no data instead of just showing the array as empty.
Note the books and authors are different tables and are linked. I thought this might be the issue but since they are linked and the GET request for the table books shows the author's array that leads me to believe it wasn't the issue but I could be wrong.
all my attempts keep consoling the message of array has data in it when it obviously does not
The author's array comes from another file called authors in a data dir but the reason it's not showing data is for another reason that would not help with this question.
Here is the GET function
//Get function
const getBooks = async (req, res) => {
try {
const books = await Book.find({});
return res.status(200).json({ success: true, message: 'you have successfuly got all the books ', data: books });
} catch (err) {
return res.status(500).json({ success: false,
msg: err.message || "Something went wrong while getting all books",
});
}
};
here is the get response in postman
{
"success": true,
"message": "you have successfully got all the books ",
"data": [
{
"_id": "625f9334ee0d5550bb041eb2",
"title": "Animal farm",
"authors": [],
"__v": 0
}
So the response comes back with the id and title and also an empty array of authors.
i have tried
const getBooks = async (req, res) => {
try {
const books = await Book.find({});
if(authors.length == null){
console.log("the authors arry is empty")
}
else{
console.log("array has data in it")
}
as well as
//Get function
const getBooks = async (req, res) => {
try {
const books = await Book.find({});
if(!authors.length == null){
console.log("the authors arry is empty")
}
else{
console.log("array has data in it")
}
And this
const getBooks = async (req, res) => {
try {
const books = await Book.find({});
if(!authors.length){
console.log("the authors arry is empty")
}
else{
console.log("array has data in it")
}
have tried
const getBooks = async (req, res) => {
try {
const books = await Book.find({});
if (typeof authors !== 'undefined' && authors.length === 0) {
console.log("array is empty");
}
else{
console.log("array has data");
and
const getBooks = async (req, res) => {
try {
const books = await Book.find({});
if (typeof authors.length === 0) {
console.log("array is empty");
}
else{
console.log("array has data");
but still says it has data
CodePudding user response:
The thing is, !authors.length
should work if your array is empty.
But i can see, you have used await Book.find({})
, so the response you will get from db is in the array not the object, hence your response structure is like :
[
{
"_id": "625f9334ee0d5550bb041eb2",
"title": "Animal farm",
"authors": [],
"__v": 0
}
]
So for this you have to put check on index of array like
const books = await Book.find({});
if(!books[0].authors.length){
console.log("the authors arry is empty")
}
Cause i cant see anywhere in your question, where the author field is coming, its probably coming from books array after db call. So try doing this, it will fix the issue.
const books = [{
"_id": "625f9334ee0d5550bb041eb2",
"title": "Animal farm",
"authors": [],
"__v": 0
}]
if (!books[0].authors.length) {
console.log('no author found')
}
Let me know if you have any doubts.