我的控制器中有这部分代码,我想要做的是当我在邮递员"getProject"
中在同一个屏幕上执行"localhost:3000/project/:id"
做一个帖子或用params把它放到它上面,但是当我尝试执行put或post时,它并没有保存在我的项目模型中
我尝试使用此代码,但并不是我想要的
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});
}
});
}
我有两个模型,第一个是"项目"
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);
第二个是"工作"模型:
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);
我想在项目控制器中执行一项功能,创建并推动" work"将数据导入现有的"项目"
答案 0 :(得分:0)
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)
答案 1 :(得分:0)
我这样做,我希望这个代码作为我的控制器
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});
}
});
}
以此作为我的模特
'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);