2012-01-20 9 views
5

Scrivo il mio primo modulo su nodejs. Ho bisogno di analizzare il mio sito dalla cache di Google. Il post è la mappa del post sul tavolo. Quando provo utilizzare questo modulo ho questo errore: "TypeError: Impossibile impostare la proprietà 'prototipo' di indefinito"? Come correggere questo errore E 'il mio codice:Come utilizzare il prototipo in node.js

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

Grazie.

risposta

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

Penso che volevi dire

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

e quindi accedere in questo modo

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

Con il primo si sta facendo exports essa stessa una funzione di chiamata , per modificarlo voi userebbe module.exports.prototype.

Rilevante materiale di studio: http://kangax.github.com/nfe/

+0

module.exports.Post = funzione Post (documentDOM, opzioni) {...} e module.exports.Post.prototype = {...} - Tutto in post. js. Quindi in app.js var Post = require ('./ post.js'); var post = new Post ({}, {}); E mi viene restituito l'errore: Object.CALL_NON_FUNCTION_AS_CONSTRUCTOR (nativo) –

+2

@ v.tsurka: 'var Post = require ('./ post.js'). Post; var post = new Post ({}, {}); 'Poiché la funzione si trova ora nella proprietà' .Post' delle esportazioni, è necessario accedervi tramite tale proprietà. –

+0

grande grazie, tutto ha funzionato bene! –

Problemi correlati