2016-11-22 123 views
1

因此,我只是第一次嘗試es6並希望使用新的類,但我遇到了一個奇怪的問題。所有類都可以正常工作,並且可以很好地寫入/讀取它們的類變量但是一個班級的表現並不好。es6類變量無法讀取(無法讀取屬性'...'的未定義)

我目前收到此錯誤:

TypeError: Cannot read property 'product' of undefined at getProducts (***/controllers/ProductController.js:41:13)

我正在寫使用的平均堆棧學校分配的REST API。

總之什麼目前發生

index.js

// dependencies 
const Api = require('./routes/api') 

class Rest 
{ 
    constructor() 
    { 
     this.api = new Api() 
     this.init() 
    } 

    init() 
    { 
     ... // express server configuration 

     // routes 
     this.routes() 
    } 

    // set routes 
    routes() 
    { 
     app.use('/api', this.api.getRouter()) 
    } 
} 

new Rest() 

/routes/api.js

// dependencies 
const express = require('express') 
const Products = require('./api/products') 

class Api 
{ 
    constructor() 
    { 
     this.router = express.Router() 
     this.products = new Products() 
     this.routes() 
    } 

    getRouter() { return this.router } 

    routes() 
    { 
     // product routes 
     this.router.use('/products', this.products.getRouter()) 
    } 
} 


// return routes 
module.exports = Api 

路線/ API/products.js

// dependencies 
const express = require('express') 
const productController = require('../../controllers/ProductController') 

class Product 
{ 
    constructor() 
    { 
     this.router = express.Router() 
     this.controller = new productController() 
     this.routes() 
    } 

    getRouter() { return this.router } 

    // set header options 
    setCollectionOptions(req, res, next) 
    { 
     res.header('Access-Control-Allow-Methods', 'POST, GET, OPTIONS') 
     next() 
    } 

    // set routes 
    routes() 
    { 
     this.router.route('/') 
      .options([this.setCollectionOptions, this.controller.getOptions]) 
      .get([this.setCollectionOptions, this.controller.getProducts]) 
    } 
} 

// return routes 
module.exports = Product 

模型/ Product.js

// dependencies 
const mongoose = require('mongoose') 
const mongoosePaginate = require('mongoose-paginate') 

// create schema for model 
const productSchema = new mongoose.Schema({ 
    name: String, 
    sku: String, 
    price: String, 
    created_at: { type: String, default: Date.now }, 
    updated_at: { type: String, default: Date.now } 
}) 
productSchema.plugin(mongoosePaginate) 

// export model 
module.exports = mongoose.model('Products', productSchema) 

控制器/ ProductController.js

// dependencies 
const express = require('express') 

class ProductController 
{ 
    constructor() 
    { 
     this.product = require('../models/product') 
    } 


    getProducts(req, res, next) 
    { 
     this.product.find() // error occurs here! 
     ... // rest of the code 
    } 
} 

this.product.find()

當我console.log(this.product)將出現錯誤,之後立即我設置它,它只是返回罰款。但是,當我通過GET請求頁面http://localhost:port/api/products時,我收到此錯誤。

另外,當我嘗試使用ProductController中的方法時,它會導致相同的錯誤。例如:

class ProductController 
{ 
    constructor() 
    { 
     this.product = require('../models/product') 
    } 

    init() 
    { 
     console.log('hi!') 
    } 

    getProducts(req, res, next) 
    { 
     this.init() 

     ... // rest of code 
    } 
} 

在此先感謝。

回答

2

在routes/api/products.js中,您只是傳入方法,這意味着當它們被調用時它們將不會有this集合。你需要約束他們,例如:

.get([this.setCollectionOptions.bind(this), this.controller.getProducts.bind(this.controller)]) 

或使用箭頭的功能,儘管這需要照顧的參數個數:

.get([(req, res, next) => this.setCollectionOptions(req, res, next), (req, res, next) => this.controller.getProducts(req, res, next)]) 

不要忘記爲傳遞給方法做到這一點。選項也是如此。

+1

雖然你需要將它綁定到'this.controller'。也許箭頭功能是一個更簡單的解決方案。 – Bergi

+0

@Bergi,謝謝,關於接收器的好消息。也添加了箭頭示例。 – Bakkot

+0

如果你不知道參數的數量或者有太多拼寫出來,你也可以使用rest/spread語法:'(... args)=> this.controller.getProducts(... args) ' – Bergi

相關問題