dimanche 7 janvier 2018

TypeError: Cannot read property 'something' of undefined. Cannot push into the array

enter image description here

app.get("/:subject", function (req, res) {
    Subject.find({name: req.params.subject}).populate("courses").exec(function (err, foundSubject) {
        if (err) {
            res.redirect("/")
            console.log(err)
        } else {

            if (err) {
                console.log(err);
            } else {
                console.log(foundSubject[0])
                console.log(foundSubject[0].courses)
                res.render("template.ejs", {subjects: foundSubject[0], courses: foundSubject[0].courses})
            }

        }
    })

})

This is the code that I wrote to see the logs whenever I visit a subject route and this is what I get.

enter image description here

So the "Python" data, there is an empty array of "courses"

and this is the new form

enter image description here

<% include partials/header.ejs%>
<div class="container">
    <div id="inputform">
        <form action="/<%=subject.name%>/courses" method="POST">
            <div class="form-group">
                <label for="title">Title:</label>
                <input type="text" id="title" name="courses[title]" placeholder="Title" style="width:600px">
            </div>

            <div class="form-group">
                <div id="wmd-button-bar"></div>
                <textarea id="wmd-input" name="courses[content]" class="wmd-input"></textarea>
            </div>

            <div class="form-group">
                <div id="wmd-preview" class="wmd-panel wmd-preview"></div>
            </div>

            <input class="btn btn-primary btn-md" type="submit">

        </form>
    </div>
</div>

//POST ROUTE FOR COURSES

app.post("/:subject/courses", function (req, res) {
    Subject.find({name: req.params.subject}, function (err, theSubject) {
        if (err) {
            res.redirect("/")
            console.log(err)
        } else {
            Course.create(req.body.courses, function (err, createdCourse) {
                if (err) {
                    console.log(err)
                } else {
                    console.log("===========theSubject===========");
                    console.log(theSubject);
                    console.log("===========theSubject[0]===========");
                    console.log(theSubject[0]);
                    console.log("===========req.body.courses===========");
                    console.log(req.body.courses);
                    console.log("===========createdCourse===========");
                    console.log(createdCourse);
                    console.log("===========");
                    theSubject[0].courses.push(createdCourse);
                    theSubject[0].save();
                    res.redirect("/:subject")
                }
            })
        }
    })
})

enter image description here

I really don't understand why I get Cannot read property 'courses' of undefined




Aucun commentaire:

Enregistrer un commentaire