2016-09-21 8 views
6

में परिभाषित किया है। मैं Node.js, Express.js और MongoDB का उपयोग करके एक ऐप बना रहा हूं। मैं एक एमवीसी पैटर्न का उपयोग कर रहा हूं और मार्गों के लिए अलग फ़ाइल भी है। मैं एक नियंत्रक वर्ग बनाने की कोशिश कर रहा हूं, जिसमें एक विधि इसके भीतर घोषित एक और विधि कहती है। लेकिन मैं ऐसा करने में सक्षम नहीं लग सकता। मुझे अपरिभाषित "संपत्ति नहीं पढ़ी जा सकती"।किसी क्लास के भीतर एक विधि को कॉल नहीं कर सकता जिसे उसने ES6 में Node.js

index.js फ़ाइल

let express = require('express'); 
let app = express(); 

let productController = require('../controllers/ProductController'); 

app.post('/product', productController.create); 

http.createServer(app).listen('3000'); 

ProductController.js फ़ाइल

class ProductController { 
    constructor(){} 

    create(){ 
    console.log('Checking if the following logs:'); 
    this.callme(); 
    } 

callme(){ 
    console.log('yes'); 
} 
} 
module.exports = new ProductController(); 

जब मैं इस चलाने मैं त्रुटि संदेश निम्नलिखित हो:

Cannot read property 'callme' of undefined 

मैं अपने आप में इस कोड को भाग गया है निम्नलिखित के रूप में थोड़ा संशोधन के साथ और यह काम करता है।

class ProductController { 
    constructor(){} 
    create(){ 
    console.log('Checking if the following logs:'); 
    this.callme(); 
    } 

    callme(){ 
    console.log('yes'); 
    } 
} 
let product = new ProductController(); 
product.create(); 

कोई काम क्यों करता है और दूसरा नहीं? सहायता!

+2

आप [एक वर्ग उदाहरण निर्यात कभी नहीं करना चाहिए यहाँ एक सरल उदाहरण समस्या का प्रदर्शन है ] (http://stackoverflow.com/a/39079929/1048572)। या तो कक्षा को स्वयं निर्यात करें, या केवल एक वस्तु का उपयोग करें। – Bergi

उत्तर

2

आपका विधि being rebound to the Layer class within express है, उसके मूल संदर्भ खोने। तरीका है कि व्यक्त मार्गों संभालती एक Layer वर्ग है, जो खुद के लिए रास्ते में कॉलबैक प्रदान करती है में से हर एक लपेटकर है:

this.handle = fn; 

है वहीं अपनी समस्याओं उत्पन्न होती हैं, इस काम के लिए स्वचालित रूप से Layer को समारोह संदर्भ rebinds।

function Example() { 
    this.message = "I have my own scope"; 
} 
Example.prototype.logThis = function() { 
    console.log(this); 
} 

function ReassignedScope(logThisFn) { 
    this.message = "This is my scope now"; 
    // simulation of what is happening within Express's Layer 
    this.logThis = logThisFn; 
} 

let example = new Example() 
let scopeProblem = new ReassignedScope(example.logThis); 

scopeProblem.logThis(); // This is my scope now 

दूसरों को पहले से ही समाधान है, स्पष्ट रूप से ProductController उदाहरण के लिए अपने विधि बाध्य करने के लिए है जो ने बताया है:

app.post('/product', productController.create.bind(productController)); 
2

जब आप create विधि के रूप में विधि को पास करते हैं तो इसे संभवतः विभिन्न संदर्भ (this) में कहा जाता है जैसा कि आप उम्मीद करते हैं। आप इसे बाध्य कर सकते हैं:

app.post('/product', productController.create.bind(productController)); 

वहाँ कई अन्य तरीकों से सुनिश्चित करने के लिए कैसे this वस्तु को दूर करने के लिए संदर्भित करता है है।

उदा। निर्माता में

app.post('/product', (...args) => productController.create(...args)); 

या बाँध तरीके:: समारोह (या तो तीर या शास्त्रीय) के साथ लपेट

constructor() { 
    this.create = this.create.bind(this); 
} 
संबंधित मुद्दे