I have a state variable projects which should store a dictionary of arrays, where the key is id of the organisation that owns the project, and the array consists of objects storing information about the project. For example:
{
orgId123: [
project1: {
name: "my cool project",
status: "submitted"
},
projectAwesome: {
name: "Stan's project",
status: "draft"
}
],
orgUSA: [
newProj234: {
name: "another great project",
status: "submitted"
}
]
}
I try to get a list of all organisation IDs using Objects.keys(projects), however that returns an empty array.
I suspect that somehow my projects variable is structured wrongly. When I console.log the contents of projects, I get:
Notice how the root level object says just {}.
When I tried to re-create what the projects variable should look like and logged that, I saw a slightly different output:
In this manually created object, the root-level object is shown as {orgId1}: Array(1) instead of the previously-shown {} (on the actual object).
What does this say about how the object is structured and why can I not get a list of keys from the first object using Object.keys()?
For context, I create the original variable using the following code:
async function fetchProjects() {
// Load list of organisations that the user is a member of
const organisationsSnapshot = await getDocs(query(
collection(db, 'organisations'),
where(`members.${user.uid}`, '!=', null)
))
const organisations = organisationsSnapshot.docs.map(organisationSnap => ({
...organisationSnap.data(),
id: organisationSnap.id
}))
// Load list of projects for each organisation the user is a member of
const projectsDict = {}
organisations.forEach(async (organisation) => {
const projectsQuery = query(collection(db, `organisations/${organisation.id}/projects`))
const projectsSnap = await getDocs(projectsQuery)
projectsDict[organisation.id] = projectsSnap.docs.map(projectSnap => ({
...projectSnap.data(),
id: projectSnap.id
}))
})
setProjects(projectsDict)
}

