'Typescript: How to post a nested object to Firestore?
I want to do a post with a nested object to Firestore.
Here I have an interface Project with overview inside:
interface Project {
projectName: string,
projectType: string,
country: string,
city: string,
overview: {
mainProject: string,
projectId: number,
m_tool_Id: string,
businessUnitId: string
}
}
And here is my interface Request:
interface Request {
body: Project,
params: { projectId: string }
}
I tried to add overview to const in *addProject* and to projectObject like I added
projectName, projectType, country, city, but I still could write everything inside overview. By that I mean that instead of these fields: mainProject, projectId, m_tool_Id, businessUnitId which I thought were required, I could paste any information.
const addProject = async (req: Request, res: Response, next: NextFunction) => {
// tried to add *overview* here
const { projectName, projectType, country, city } = req.body
try {
const project = firestore.collection('projects').doc()
const projectObject = {
id: project.id,
projectName,
projectType,
country,
city
}
project.set(projectObject)
res.status(200).send({
status: 'success',
message: 'project added successfully',
data: projectObject
})
} catch (error) {
next(error);
}
}
The goal was to get something like this in Firestore:
How can I get and require fields from a nested object and how to post them with the main object?
Sorry if that was too much info, but I just want to learn how to handle nested objects.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

