Dariun
Dariun

Reputation: 347

Handlebars: Access has been denied to resolve the property "name" because it is not an "own property" of its parent

I got the issue with handlebars 4.7.3. I already checked the solution from this ask, Handlebars: Access has been denied to resolve the property "from" because it is not an "own property" of its parent but it was no solution for my code so I hope someone can help me.

Controller.js

submitPosts: (req, res) => {
        // Check the attributs from create.handlebars for success or error message
        const newPost = new Post( {
            surname: req.body.surname,
            name: req.body.name,
            biography: req.body.biography,
            profilpicture: req.body.profilpicture,
            paintings: req.body.paintings,
        });
        // Safe new posts
        newPost.save().then(post => {
            console.log(post);
            flash('success-message', 'new post!')
            res.redirect('/admin/posts');
        });

    },

postModel.js

const
    mongoose = require('mongoose');

const Schema = mongoose.Schema; // get props

const PostSchema = new Schema({
    // define props --> required for a post

    surname: {
        type: String,
        default: ''
    },
    name: {
        type: String,
        default: ''
    },
    biography: {
        type: String,
        default: ''
    },
    profilpicture: {
        type: Object,
    },
    paintings : {
        type: Object,
    }


});

module.exports = mongoose.model('post', PostSchema);

index.handlebars

{{#each post}}
            <tr>
                <td>{{ surname }}</td>
                <td>{{ name }}</td>
                <td><img style="width: 100px; height:100px;" src="{{ profilpicture }}"></td>
                <td>{{ biography }}</td>
                <td><img style="width: 100px; height:100px;" src="{{ paintings }}"></td>
            </tr>
            {{/each}}

Already tried every possibility from the other ask on stack overflow, other handlebars version, change router code, ... NOTHING WORKS :(

Upvotes: 0

Views: 1530

Answers (1)

golfo
golfo

Reputation: 85

Just had the same issue that wrecked my nerves. Tried additional packages etc but in the end a simple command solve this. Its the Command ".lean()" Found more here: link to mongoose docs for lean()

my code example:

// show message page Route
router.get("/", (req, res) => {
//read all Message entries from db
   Message.find() 
   //sort them descending by date property
   .sort({ date: "desc" })
   .lean()
   //take these and call the rendered page and pass them as object to the page
   .then(messages => {
     res.render("messages/show_messages", { messages: messages });
   });
});

Upvotes: 1

Related Questions