Here's a scenario where I have set up a post route:
router.post("/projects", async (req, res) => {
const {
projectName,
projectDescription,
projectBudget,
projectDuration,
industry,
companyName,
numberOfEmployees,
diamond,
} = req.body;
console.log(diamond);
const [projectDiamond] = diamond;
const { criteria } = projectDiamond;
if (
!projectName ||
!projectDescription ||
!projectBudget ||
!projectDuration ||
!industry ||
!companyName ||
!numberOfEmployees ||
!diamond
) {
return res.status(422).send({ error: "Must provide all project details" });
}
try {
const project = new Project({
projectName,
projectDescription,
projectBudget,
projectDuration,
industry,
companyName,
numberOfEmployees,
diamond,
userId: req.user._id,
});
const recommendation = await Recommendation.find({
"diamond.criteria": criteria,
});
const projectsWithSameDiamond = await Project.find({
"diamond.criteria": criteria,
});
const projectsWithSameIndustry = await Project.find({ industry });
await project.save();
} catch (err) {
res.status(422).send({ error: err.message });
}
});
Let's discuss how to utilize these data in a React Native component. Starting with a component called A.js:
const A = () => {
return(
//returning something here
)
)
}
If this component uses axios to send a POST request to the defined route:
const A = () => {
...
axios.post("/projects", {projectName,
projectDescription,
projectBudget,
projectDuration,
industry,
companyName,
numberOfEmployees,
diamond} );
return(
//returning something here
)
)
}
Once the request is successful and a project is posted, how can we display those variables on screen?
const A = () => {
return(
<Text>{recommendation}</Text> {/*Not sure how to get this after posting a new project */}
<Text>{projectWithSimilarDiamond}</Text> {/*Not sure how to get this after posting a new project */}
<Text>{projectWithSimilarIndustry}</Text> {/*Not sure how to get this after posting a new project */}
)
)
}