[英]how can I do a push to a document in mean stack project?
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 我在控制器中有这部分代码,我要做的是当我在同一屏幕上的邮递员"localhost:3000/project/:id"
中执行"getProject"
,用参数将其张贴或放置在上面,但是当我尝试放置或发布时,它不会保存在我的项目模型中
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 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 模型.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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.