ES6:“ TyperError:X不是函数”

时间:2019-04-07 17:53:35

标签: javascript node.js webpack babeljs es6-class

我正在尝试向Express / Node服务器中的类添加函数,但是我一直在获取TypeError: req.session.user.bills[0].getFormattedDueDate is not a function。我还有其他类的成员函数可以正常工作,包括引发该错误的相同方法。我检查了req.session.user.bills中对象的类型,它是object。该项目是使用Webpack和Babel构建的。

如果我将req.session.user.bills[0]中的所有属性都放入一个新对象中,则这些函数将起作用。该对象的数据流如下所示。主文件的名称为server2.js

  1. 用户通过点击server2.js中的终结点来登录。
  2. 对象名称userDao从数据库中吸引用户,并返回带有User数组的Bill对象。 UserDaoUser都位于require的顶部server2.js
  3. 点击另一个端点以显示账单。这会将User对象传递到EJS文件。我在这里尝试使用该功能,但它不起作用。

server.js

const User = require('babel-loader!./src/model/user.js');
const UserDao = require('babel-loader!./src/model/userDao.js');
const Bill = require('babel-loader!./src/model/bill.js');

const userDao = new UserDao(pool);
...
app.all('/bills', function(req, res) {
    const temp1 = req.session.user.bills[0].getFormattedDueDate();
    res.render('bills.ejs', {
        URL: config.URL,
        user: req.session.user
    })
});

但是,如果我实例化一个新对象,它将起作用:

app.all('/bills', function(req, res) {
    const temp = new Bill(
        req.session.user.bills[0].id, 
        req.session.user.bills[0].userId, 
        req.session.user.bills[0].periodId, 
        req.session.user.bills[0].accountId, 
        null, 
        req.session.user.bills[0].name, 
        req.session.user.bills[0].amount, 
        req.session.user.bills[0].autoPay, 
        req.session.user.bills[0].weekDay, 
        req.session.user.bills[0].dueDate, 
        req.session.user.bills[0].dueDate2, 
        req.session.user.bills[0].paid
    );
    const temp1 = temp.getFormattedDueDate();
    res.render('bills.ejs', {
        URL: config.URL,
        user: req.session.user
    })
});

userDao.js

//Lots of other data is put in user...
//Bills
for (let i = 0; i < rows[BILLS_INDEX].length; i++) {
    user.bills.push(new Bill(
        rows[BILLS_INDEX][i].id,
        rows[BILLS_INDEX][i].userId,
        rows[BILLS_INDEX][i].periodId,
        rows[BILLS_INDEX][i].accountId,
        new Tag(rows[BILLS_INDEX][i].tagId, rows[BILLS_INDEX][i].userId, rows[BILLS_INDEX][i].name),
        rows[BILLS_INDEX][i].name,
        rows[BILLS_INDEX][i].amount,
        rows[BILLS_INDEX][i].autoPay === 1,
        rows[BILLS_INDEX][i].weekDay === 1,
        rows[BILLS_INDEX][i].startDate,
        rows[BILLS_INDEX][i].startDate2,
        rows[BILLS_INDEX][i].paid === 1
    ));
}

resolve(user);

user.js

module.exports = class User {
    constructor(id, firstName, lastName, imageUrl, email, tags, items, budgetItems, piggyBanks, bills) {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
        this.imageUrl = imageUrl;
        this.email = email;
        this.tags = tags || new Array();
        this.items = items || new Array();
        this.budgetItems = budgetItems || new Array();
        this.piggyBanks = piggyBanks || new Array();
        this.bills = bills || new Array();
    }
}

bill.js

const moment = require('moment');

module.exports = class Bill {
    constructor(pId, pUserId, pPeriodId, pAccountId, pTag, pName, pAmount, pAutoPay, pWeekDay, pdueDate, pdueDate2, pPaid) {
        this.id = pId;
        this.userId = pUserId,
        this.periodId = pPeriodId,
        this.accountId = pAccountId,
        this.tag = pTag,
        this.name = pName,
        this.amount = pAmount,
        this.autoPay = pAutoPay,
        this.weekDay = pWeekDay,
        this.dueDate = pdueDate,
        this.dueDate2 = pdueDate2,
        this.paid = pPaid
    }

    getFormattedDueDate() {
        return moment(this.dueDate).format('MMM DD, YYYY');
    }

    getFormattedDueDate2() {
        return moment(this.dueDate2).format('MMM DD, YYYY');
    }
}

由于userDao.js中的方法可以工作,所以我希望该方法可以工作,但是我得到未定义的方法错误。

编辑:我检查了req.session.user.bills[0] instanceof Bill,它返回了false。

1 个答案:

答案 0 :(得分:0)

将对象存储在session中时,该对象将序列化为JSON。当您检索它时,会将它再次解析为一个普通对象。因此,任何原始原型信息都会在此过程中丢失。如https://pypi.org/project/librosa/的文档所述:

  

要存储或访问会话数据,只需使用请求属性req.session,该属性通常由商店[...]序列化为JSON [...]

要恢复原型信息,您可以执行以下操作:

req.session.user.bills.forEach(bill => Object.setPrototypeOf(bill, Bill.prototype));

然后这应该起作用:

const temp1 = req.session.user.bills[0].getFormattedDueDate();

或者,如果只想调用一次该方法,则可以使用.call

const temp1 = Bill.prototype.getFormattedDueDate.call(req.session.user.bills[0]);