2017-09-04 3 views
0

Ich habe diesen Teil des Codes in meinem Controller, was ich tun möchte, ist, wenn ich eine "getProject" in Postman "localhost:3000/project/:id" im selben Bildschirm mache einen Beitrag oder einen über ihn mit den Parametern, aber wenn ich versucht, den Put zu tun oder per Post nicht in meinem Projektmodell nicht speichernWie kann ich einen Push zu einem Dokument im Mean-Stack-Projekt machen?

ich mit diesem Code versucht, aber nicht tun, was ich will

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}); 
     } 
    }); 
} 

ich habe zwei Modelle, die firstone ist „Projekt“

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); 

und der seco nd ist „Arbeit“ Modell:

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); 

und ich möchte eine Funktion in Projektcontroller zu tun, erstellen und drücken Sie die „Arbeit“ Daten in bestehende „Projekt“

Antwort

0

ich tun, was ich will, mit diesem Code als mein 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}); 
     } 
    }); 

} 

und mit dieser als mein Modell

'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); 
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) 
+0

es mir einen Fehler senden, auf den „callback.apply ist keine Funktion“, so habe ich eine andere Frage bekam, wenn kein Problem ist, hehe, dass ich Willst du tun, ist, habe ich zwei Modelle "Projekt" und "Arbeit" so Projekt haben Name, offizielleName, etc. und die "Arbeit" -Array, also möchte ich "arbeiten" Daten in Arbeit Array im Projekt, danke, Ich bearbeite meinen Beitrag hehe –

+0

dieser Fehler könnte wegen '{upsert: true}' auftreten, entfernen Sie es und führen – Vicky

+0

es nicht die Arbeit, die erste muss ich tun "Projekt erstellen" also wenn ia Haben Sie das Projekt mit allen Daten erstellt "name, offizielleName, Preis, startDate, endDate, Vertrag, Arbeit: []. " also danach muss ich eine Arbeit erstellen, die in Arbeit Array in Projekt speichert jede Arbeit, die ich in diesem Projekt erstellt habe muss speichern in funktioniert Array –

Verwandte Themen