Hi,
I'm new to nodejs, i would like to write the code for something like this.
/project/:_id/eventid/event:_id/dim/:dim_id
I want to POST the data into mogodb, I m using mongoose. I hv written code
upto /project/:_id/.
but the thing i want to know is how to write a schema for this
Project name (unique) *
ProjectId(auto indexed)
Project Description /project/getbyname
Events [ "ids"] (array) /project/<projectID>/addevent
Dimensions[]() /project/<projectID>/adddim
Customers[] /project/<projectID>/addcustomer
for the first time when i post project name, it doesnt contain any event
ids, dimensionids and all.
afterwards, if i want to add eventids i could be able to do that and all
the information i posted into the database should be seen when i give the
path /projects/:id using GET Method.
for example: /project/<projectID> using GET method i should be able to
get like this.
projectname: alpha
ProjectId: 5aba228af798ab256796313a
Project Description: alpha Project
Events [ "ids"]: [
"oiuytr56789", "kjhgf98765yu",
]
Dimensions: [
"llllllll89970pp", "hoed78uiouyhn00"
]
Customers: [
"intel", "wipro"
]
and i want to POST individual eventId using /project/<projectId>/addeventid
for example: using POST method
/project/5aba228af798ab256796313a/addeventid/
REQUEST:
EVENTS : [
eventid: "ggggggggooooopppaaalllaaa"
]
when i use GET method for the /projects/ .
Response should be something like this:
projectname: alpha
ProjectId: 5aba228af798ab256796313a
Project Description: alpha Project
Events [ "ids"]: [
"oiuytr56789", "kjhgf98765yu", "ggggggggooooopppaaalllaaa"
]
Dimensions: [
"llllllll89970pp", "hoed78uiouyhn00"
]
Customers: [
"intel", "wipro"
]
code I hv written until now...
==========================
schema for Projects
const mongoose = require('mongoose');
const projectSchema = mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId,
projectName: { type: String, required: true, unique:true },
projectDescription: { type: String, required: false },
dimensions: { type: [], required: false },
events: { type: [], required: false },
customers: {type: [],required: false}
});
module.exports = mongoose.model('Project', projectSchema);
to POST and GET method logic:(it also contains delete, getById etc).
const express = require("express");
const router = express.Router();
const mongoose = require("mongoose");
const Project = require("../models/project");
router.get("/", (req, res, next) => {
Project.find()
.exec()
.then(docs => {
const response = {
count: docs.length,
projects: docs.map(doc => {
return {
projectName: doc.projectName,
projectDescription: doc.projectDescription,
dimensions: doc.dimensions,
events: doc.events,
_id: doc._id
};
})
};
res.status(200).json(response);
})
.catch(err => {
console.log(err);
res.status(500).json({
error: err
});
});
});
router.post("/", (req, res, next) => {
const project = new Project({
_id: new mongoose.Types.ObjectId(),
projectName: req.body.projectName,
projectDescription: req.body.projectDescription,
dimensions:req.body.dimensions,
events: req.body.events
});
project
.save()
.then(result => {
console.log(result);
res.status(201).json({
message: "Created project successfully",
createdProject: {
projectName: result.projectName,
projectDescription: result.projectDescription,
_id: result._id,
events: result.events
}
});
})
.catch(err => {
console.log(err);
res.status(500).json({
error: err
});
});
});
router.get("/:projectId", (req, res, next) => {
const id = req.params.projectId;
Project.findById(id)
.exec()
.then(doc => {
console.log("From database", doc);
if (doc) {
res.status(200).json({
project: doc,
});
} else {
res
.status(404)
.json({ message: "No valid entry found for provided ID" });
}
})
.catch(err => {
console.log(err);
res.status(500).json({ error: err });
});
});
/*
router.patch("/:projectId", (req, res, next) => {
const id = req.params.projectId;
const updateOps = {};
for (const key of Object.keys(req.body)) {//for (const ops of req.body)
{updateOps[ops.propName] = ops.value;}
updateOps[key] = req.body[key]
}
Project.update({ _id: id }, { $set: updateOps })
.exec()
.then(result => {
res.status(200).json({
message: 'Project updated'
});
})
.catch(err => {
console.log(err);
res.status(500).json({
error: err
});
});
});
*/
router.delete("/:projectId", (req,res,next) =>{
const id = req.params.projectId;
Project.remove({_id : id})
.exec()
.then(result =>
res.status(200).json({
message: 'Project deleted'
}))
.catch(err =>{
console.log(err);
res.status(500).json({
error:err
});
});
});
router.get("/P/:projectName", (req, res, next) => {
const projectName = req.query.projectName;
Project.findOne(projectName)
.select('_id')
.exec()
.then(doc => {
console.log("From database", doc);
if (doc) {
res.status(200).json({
project: doc,
});
} else {
res
.status(404)
.json({ message: "No valid entry found for provided name of Project" });
}
})
.catch(err => {
console.log(err);
res.status(500).json({ error: err });
});
});
router.post("/:projectId/addEvent/", (req, res, next) => {
const id = req.params.projectId;
const project = new Event({
_id: new mongoose.Types.ObjectId(),
events: req.body.events
});
project
.save()
.then(result => {
console.log(result);
res.status(201).json({
message: "Created Event successfully",
createdProject: {
events: result.events
}
});
})
.catch(err => {
console.log(err);
res.status(500).json({
error: err
});
});
});
module.exports = router;
right now i m unable to find a way write schema for how to Post the data
inside an array.
ANY HELP is appreciated.
Thanks,
DARSHAN
--
Job board: http://jobs.nodejs.org/
New group rules:
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/nodejs/36ee6421-fc5a-400b-8fd1-9977e0be4d39%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.