-1

Currently my Api is returning an object instead of an object within an array, however I can't seem to tell the difference between these two routes and why one would return said array of data over the other.
For instance :

router.get('/caseDetail/:id', (req,res) => {
db.caseDetail.findOne({
    include : [db.Part,db.Fault,db.Disposition,db.Contact,db.Site]
}).then((response) => {
    res.json(response);
}).catch((error) => {
    console.table([stack.error,stack.id,error])
})
})

The above route returns an array of data while the following returns just an object

router.get('/caseDetail/:caseName', (req,res) => {
    db.caseDetail.findAll({
        
        include : [db.Part,db.Fault,db.Disposition,db.Contact,db.Site],
        where : {
            caseName:{ 
              [Op.like]: req.params.caseName}
        }
    }).then((response) => {
        console.log(response);
        res.json(response)
    }).catch((error) => {
        console.log(error);
    })
})

-------------------------- For context----------------------------
I've enacted this method multiple times, even in other script files, but I haven't been required to parse data in this manner, is something out of shape here that im over looking? Am I missing a JSON.parse(); here? Github

  try { 
                  const items = await axios.get(`/api/caseDetail/:caseName` + caseName);
                console.log(items.data); 
                $tbody.empty()
                items.data.forEach((item) => {
                    console.log(item);

Returned Results

{id: 2, caseName: "1 - Fenway Park - 1", createdAt: "2021-07-27T18:13:55.000Z", updatedAt: "2021-07-27T18:13:55.000Z", UserId: 1, …}

Error Message

TypeError: items.data.forEach is not a function
    at callSearchResults (searchInventory.js:29)
BlakeT
  • 59
  • 9
  • Thanks. Looks like an object so you can't use forEach. – Invizi Sep 16 '21 at 21:20
  • these results are returned after the call to my API route, giving me the data, yet it wont loop through it, sure theres only 1 item that's being returned as only one thing exists in the DB at the moment, yet this error shouldn't persist. – BlakeT Sep 16 '21 at 21:21
  • You need to return the object in an array, if you want to loop using .forEach() – Invizi Sep 16 '21 at 21:22
  • It's an array of objects, the console you're specifically seeing is a log of items.data – BlakeT Sep 16 '21 at 21:23
  • the log you showed, isn't an array. But an object. – Invizi Sep 16 '21 at 21:24
  • So the issues exists more so on my api route and not the function call itself? – BlakeT Sep 16 '21 at 21:38
  • Yes, because it's not returning an array of objects. Just an object. – Invizi Sep 16 '21 at 21:41
  • eh, not technically an issue with the api route, if it's a route meant to return one "item", it shouldn't be an array (and therefore there wouldn't likely be a reason to even use forEach) – Kevin B Sep 16 '21 at 21:52
  • the route is meant to return all that fit the search params entered by user, which previously only one existed so it kind of made sense that it wasn't an array. Not sure why I was downvoted for clarity when the question is outlined well. – BlakeT Sep 16 '21 at 21:59

2 Answers2

0

If I understand you correctly and this is the result that you receive from your request:

{id: 2, caseName: "1 - Fenway Park - 1", createdAt: "2021-07-27T18:13:55.000Z", updatedAt: "2021-07-27T18:13:55.000Z", UserId: 1, …}

Then you would need to take the entries out of it and iterate over them like so:

for (const [key, value] of Object.entries(items)) {
  console.log(`${key}: ${value}`);
}

Or like this:

Object.entries(items).forEach(([key, val]) => console.log(key, val));
-3

forEach() is designed for arrays but you can typecast collections and objects into arrays like this...

var I=[...items];

var D=[...items.data]

Now you should be OK, try...

I.data.forEach((item) => {}

D.forEach(() => {}