5

I write my first module on nodejs. I need parse my site from google cache. Post is map of table post. When i try use this module i have this error: "TypeError: Cannot set property 'prototype' of undefined" How fix this error?It's my code:

module.exports = function Post(documentDOM,options)
{
    this.opts = $.extend({id:0,author_id:0},options);
    this.doc = documentDOM;
    this.post  = {
        id: 0,
        name: '',
        alt_name: '',
        notice: '',
        content: '',
        author: '',
        author_id: 0,
    };
}

module.exports.Post.prototype = {
    init: function() {
        this.post.id = this.opts.id;
        this.post.author_id = this.opts.author_id;
    },

    content: function() {
        content = this.doc.find('.fullnews-content').html();
        if(!content.length)
            content = doc.find('.article-content').html();
        return content;
    }
}

Thank.

1 Answer 1

6
module.exports = function Post((documentDOM,options)

I think you meant

module.exports.Post = function((documentDOM,options)

And then access it like this

var Post = require('./post.js').Post;

With the first you're making exports itself a named function, to modify it you would use module.exports.prototype.

Relevant study material: http://kangax.github.com/nfe/

Sign up to request clarification or add additional context in comments.

2 Comments

module.exports.Post = function Post(documentDOM,options) { ... } and module.exports.Post.prototype = {...} - It all in post.js. Then in app.js var Post = require('./post.js'); var post = new Post({},{}); And thay throw me error : Object.CALL_NON_FUNCTION_AS_CONSTRUCTOR (native)
@v.tsurka: var Post = require('./post.js').Post; var post = new Post({},{}); Since the function is now located at the .Post property of the exports, you need to access it via that property.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.