These are chat archives for Automattic/mongoose

4th
Feb 2018
Dušan Todorović
@krckyboy
Feb 04 2018 09:56

I want to edit a professor and change the class (predmet) that he's teaching.. I can change everything but the class (predmet), because I dunno how to take the ID of a class and push into Professor's predmet section (which references to the predmet collection).

The PUT route is not correct, since I added predmet_.id, which the console says it's not defined.

I would REALLY appreciate the help, guys. Thank you.

Professor model:

var mongoose = require("mongoose")

//schema setup
var professorSchema = new mongoose.Schema({
    name: String,
    email: String,
    phone: String,
    address: String,
    created: {type: Date, default: Date.now},
    image: {type: String, default: "https://www.proz.com/images/128_profile_placeholder.png"},
    predmet: [
        { 
            type: mongoose.Schema.Types.ObjectId,
            ref: "Predmet"
        }
    ]
});


// compiling schema into a model with capital C
// var Campground = mongoose.model("Campground", campgroundSchema)

module.exports = mongoose.model("Professor", professorSchema)

Predmet model:

var mongoose = require("mongoose")

var predmetSchema = new mongoose.Schema({
    name: String, 
    day: String,
    term: String,
    duration: Number
})

module.exports = mongoose.model("Predmet", predmetSchema)

Update route for professor

//UPDATE route professor
app.put("/professors/:id", function(req, res){
    Professor.findByIdAndUpdate(req.params.id, req.body.professor, function(err, updatedProfessor){
        if(err){
            console.log(err)
            res.redirect("/professors")
        } else { 
            Predmet.find({}, function(err, predmets){
                if(err){
                    console.log(err)
                } else {
                    updatedProfessor.predmet.remove()
                    updatedProfessor.predmet.push(predmet_.id)
                    updatedProfessor.save(function(err, data){
                        if(err){
                            console.log(err)
                        } else {
                            res.redirect("/professors/" + req.params.id)
                        }
                    })
                }
            })

        }
    })
})

Edit professor route

// EDIT professor
app.get("/professors/:id/edit", function(req, res){
    Professor.findById(req.params.id, function(err, foundProfessor){
            if(err){
                res.redirect("/professors")
            } else {
                Predmet.find({}, function(err, predmets){
                    if(err){
                        console.log("ERROR")
                    } else {
                        res.render("editProfessor", {professor: foundProfessor, predmets: predmets});
                    }
                })
            }
    })
})

And the edit professor template

<% include ./partials/header%>

<div class="ui main text container segment">
    <div class="ui huge header">Izmena naloga - <%=professor.name%></div>
    <form class= "ui form" action="/professors/<%=professor._id%>?_method=PUT" method="POST">
        <div class="field">
            <input type="text"     name="professor[name]" value="<%=professor.name%>">
        </div>
        <div class="field">
            <input type="email" name="professor[email]" value="<%=professor.email%>">
        </div>
        <div class="field">
            <input type="text"    name="professor[phone]" value="<%=professor.phone%>">
        </div>
        <div class="field">
            <input type="text"    name="professor[image]" value="<%=professor.image%>">
        </div>
        <div class="field">
            <label>Predmet koji predaje:</label>
            <select>
                <% predmets.forEach(function(predmet){ %>
                    <option value="<%=predmet.id%>" name="<%=predmet.id%>"><%=predmet.name%></option>
                <%}) %>
            </select>
        </div>
        <button class="ui orange basic button big" type="submit">Izmeni podatke</button>
        <button class="ui red basic button big" type="reset">Resetuj polja</button>
    </form>
</div>

<script> 

</script>
<% include ./partials/footer%>
Kev
@lineus
Feb 04 2018 11:12
@krckyboy try predmet._id not predmet_.id
Dušan Todorović
@krckyboy
Feb 04 2018 13:02
@lineus
events.js:183
      throw er; // Unhandled 'error' event
      ^

ReferenceError: predmet is not defined
    at C:\Users\krcky\Desktop\WebApp-eProfessor\app.js:229:36
    at model.Query.<anonymous> (C:\Users\krcky\Desktop\WebApp-eProfessor\node_modules\mongoose\lib\model.js:3928:16)
    at C:\Users\krcky\Desktop\WebApp-eProfessor\node_modules\kareem\index.js:297:21
    at C:\Users\krcky\Desktop\WebApp-eProfessor\node_modules\kareem\index.js:135:16
    at _combinedTickCallback (internal/process/next_tick.js:131:7)
    at process._tickCallback (internal/process/next_tick.js:180:9)
:(
Kev
@lineus
Feb 04 2018 13:07
@krckyboy is the current state of your project on github? if so, I'll take a look at it. alternatively you can create a gist with the files above in their current state as well as your app.js file and I'll take a look at that instead.
Dušan Todorović
@krckyboy
Feb 04 2018 13:08
@lineus Awesome! I'll check out gist right away.
@lineus Actually it might be easier to push to github.
But I forgot how to use it, hm.
Kev
@lineus
Feb 04 2018 13:10
cool, just post the link here when it's done and I'll take a look :)
Here you go, bud.
My plan in the put professor route is to remove the predmet (means class) array values to make sure that the IDs of the predmet don't duplicate. So if I change what class he's teaching, using the select tag, I should clear the existing values first.
Also, the seeds file, I haven't bothered to add different classes to each professor, and I probably wouldn't know how to, since I am following a course on Udemy, and that's how the guy did it.
Kev
@lineus
Feb 04 2018 13:58
So if I change what class he's teaching, using the select tag, I should clear the existing values first.
this to me, sounds like he's only ever going to be teaching one class. is that right?
Dušan Todorović
@krckyboy
Feb 04 2018 13:59
@lineus The former plan was to allow professors to teach many, but I simplified it and thought they should be allowed to teach only one. I simplified it since deadline is Tuesday.
@lineus So yes, one teacher can teach only one class(predmet).
My difficulty right now is how to clear all the referenced IDs from the professor DB data, specifically the predmet field, and then push a new class through the select HTML tag, but when I do that, I only add null values.
Kev
@lineus
Feb 04 2018 14:03
if it's only going to be one, then you don't need to use an array there. your schema can just have an objectid ref to the current class the professor is teaching ( instead of an array of them ). I'll dig into the code now, I just wanted to make sure I understood what you're after.
Dušan Todorović
@krckyboy
Feb 04 2018 14:06
@lineus I added a readme file there with some basic explanation.
@lineus On Github I mean
Kev
@lineus
Feb 04 2018 14:15
cool, I just cloned your repo. I'll take a look :)
Dušan Todorović
@krckyboy
Feb 04 2018 14:20
@lineus Thank you. :)
Dušan Todorović
@krckyboy
Feb 04 2018 16:02
@lineus Any luck? :D