调用类方法时,'this'未定义

时间:2017-03-28 12:41:14

标签: javascript node.js express

我正在尝试向用户设置响应消息,但是当我这样做时,我收到错误消息。 (无法读取未定义的属性'message')这是不起作用的代码:

server.js

const server = express();
let  App =  require('./controllers/app');
App = new App();
server.use('/', App.router);

http.createServer(server).listen(8080);

app.js

class App {
    constructor () {
        this.message = '';
    } 

    router (request, response) {
        if (request.url === '/register') {
            response.render('register.ejs', {
                url: request.url,
                header: headBase + 'register',
                message: this.message, //  This is where the error is triggered
                user: username
            });
            this.message = '';
        }
    }
}

我尝试在路由器方法中设置或读取this.message的任何地方都会收到错误。

3 个答案:

答案 0 :(得分:1)

问题出在这里

let  App =  require('./controllers/app');
App = new App();
server.use('/', App.router);

在深入探讨真正的问题之前,我还要提到用{em>实例覆盖App是一个坏主意,所以我要将其重写为:

const App = require('./controllser/app')
const app = new App();
server.use('/', app.router);

现在更清楚的是,您传入的内容是来自App实例的方法引用。

问题是你正在传递函数,但函数没有以任何方式绑定到app。您可以通过多种方式解决此问题。

最简单的方法是将app.router.bind(app)传递给app.routerapp.router上下文中调用app的函数:

server.use('/', app.router.bind(app))

下一个最简单的(因为你正在使用ES2015功能)是将函数包装在lambda中:

server.use('/', (...args) => app.router(...args))

答案 1 :(得分:0)

我设法绕过它。我刚刚在路由器方法中将App.message更改为class App { constructor () { this.message = ''; } router (request, response) { if (request.url === '/register') { response.render('register.ejs', { url: request.url, header: headBase + 'register', message: App.message, user: username }); App.message = ''; } } }

<tfoot>
{assign var='totalQty' value=0}
{assign var='totalReductonPercent' value=0}
{assign var='totalReductonValue' value=0}
{assign var='totalWeight' value=0}

<!-- VALUE A VARIABLES ASSIGNATION -->
{foreach $products as $product}
{assign var='totalReductonValue' value=$totalReductonValue + $product.price_without_specific_price}
{assign var='totalQty' value=$totalQty + $product.quantity}
{/foreach}

<!-- DISCOUNT RULES ACCORDING QTY -->
{if $productNumber>=16} 
{assign var='totalReductonPercent' value=20}
{else} 
{if $productNumber>=11} 
{assign var='totalReductonPercent' value=15}
{else}
{if $productNumber>=6} 
{assign var='totalReductonPercent' value=10}
{else}
{if $productNumber>=3} 
{assign var='totalReductonPercent' value=5}
{/if}
{/if}
{/if}
{/if}

<!--  TOTAL DISCOUNT  -->
<tr class="cart_total_discount">
<td colspan="3" class="text-right">
{l s='Total discount for ('}
<span class="footer-counter">
<span id="summary_products_quantity">{$productNumber} 
{if $productNumber == 1}
{l s=') Carton: '}
{else}
{l s=') Cartons: '}
{/if}
</span>
</span>
</td>
<td colspan="2" class="price" id="total_discount">
{assign var='totalReductonValue' value=$total_products * ($totalReductonPercent/100)}
{convertPrice price=$totalReductonValue}<br>
<span class="price-percent-reduction small">
{($totalReductonPercent)|round|string_format:"%d"}%
</span>
</td>
</tr>

答案 2 :(得分:0)

zzzzBov解决方案的替代方法是绑定构造函数中的方法:

class App {
    constructor () {
        this.message = '';
        this.router = this.router.bind(this)
    } 

    router (request, response) {
        if (request.url === '/register') {
            response.render('register.ejs', {
                url: request.url,
                header: headBase + 'register',
                message: this.message,
                user: username
            });
            this.message = '';
        }
    }
}