I have this part of code in my controller, what I want to do is when I do a "getProject"
in postman "localhost:3000/project/:id"
in the same screen do a post or a put over it with the params, but when I tried to do the put or post it doesn't save in my project model
I tried with this code but doesn't do that I want
function createWork(req, res) {
var work = new Work();
var project = new Project();
var projectId = req.params.id;
var params = req.body;
obra.name = params.name;
obra.oficialName = params.oficialName;
obra.price = params.price;
obra.workType= params.workType;
obra.ubication = params.ubication;
console.log(params);
Project.update({"Title":"project"}, {
$push: {
"work": {
"name":'name',
"oficialName":'oficialName',
"price":'price',
"workType":'workType',
"ubication":'ubication',
}
}
},
{safe: true, upsert: true},
function(err, updProject){
if (err) {
res.status(404).send({message: 'Error'});
} else {
res.status(200).send({project: updProject});
}
});
}
I have two models, the firstone is "project"
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var Work = require('../models/work');
var ProjectsSchema = Schema({
name: String,
officialName: String,
price: String,
startDate: String,
endDate: String,
contract: String,
works: [{ type: Schema.Types.Object, ref: 'Work'}]
});
var Project = mongoose.model('Project', ProjectsSchema);
and the second is "work" model:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var WorksSchema = Schema({
name: String,
officialName: String,
price: String,
workType: String,
ubication: String
});
module.exports = mongoose.model('Work', WorksSchema);
and I want to do a function in project controller that create and push the "work" data into existing "project"
Controller.js
function createWork(req, res) {
var newProject = new Project();
var name = req.body.name;
var oficialName = req.body.oficialName;
var price = req.body.price;
var workType= req.body.workType;
var ubication = req.body.ubication;
var dataToPush = {
name : name,
officialName : officialName,
price : price,
workType : workType,
ubication : ubication
}
newProject.update({"Title":"project"}, {
$push: {
"work": dataToPush
}
},
{new : truesafe, safe : true, upsert : true},
function(err, updProject){
if (err) {
res.status(404).send({message: 'Error'});
} else {
res.status(200).send({project: updProject});
}
});
}
Model.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var WorkSchema = new mongoose.Schema({
name : String,
officialName : String,
price : String,
workType : String,
ubication : String
},{timestamps:true})
var ProjectSchema = new mongoose.Schema({
projectName : String,
work : [WorkSchema]
},{timestamps:true});
module.exports = mongoose.model('project', ProjectSchema)
I do that I want, with this code as my controller
function createWork(req, res) {
Project.findByIdAndUpdate((req.params.id), {
$push: {
"works": req.body
}
},
function(err, updProject) {
if (err) {
res.status(404).send({message: 'Error'});
} else {
res.status(200).send({project: updProject});
}
});
}
and with this as my model
'use strict'
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var WorkSchema = new mongoose.Schema({
name: String,
officialName: String,
price: String,
workType: String,
ubication: String
},{timestamps:true});
var ProjectsSchema = new mongoose.Schema({
name: String,
officialName: String,
price: String,
contract: String,
start: String,
end: String,
works: [WorkSchema]
},{timestamps:true});
module.exports = mongoose.model('Project', ProjectsSchema);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.