所以我第一次潜入es6并希望使用新的Classes,但我遇到了一个奇怪的问题。所有类都可以正常工作,并且可以很好地写/读它们的类变量。但是有一节课表现不佳。
我目前收到此错误:
TypeError: Cannot read property 'product' of undefined
at getProducts (***/controllers/ProductController.js:41:13)
我正在使用MEAN Stack为学校作业编写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)
时,在我设置之后,它会立即返回。但是当我在http://localhost:port/api/products
请求带有GET的页面时,我收到此错误。
此外,当我尝试在ProductController中使用方法时,会导致相同的错误。例如:
class ProductController
{
constructor()
{
this.product = require('../models/product')
}
init()
{
console.log('hi!')
}
getProducts(req, res, next)
{
this.init()
... // rest of code
}
}
提前致谢。
答案 0 :(得分:3)
在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)])
不要忘记为传递给.options的方法执行此操作。