ES6: "TyperError: X is not a function"

ES6: "TyperError: X is not a function"

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

如果我获取 req.session.user.bills[0] 中的所有属性并将它们放入一个新对象中,那么这些函数就会起作用。该对象的数据流是这样工作的。主文件的名称是 server2.js.

  1. 用户在 server2.js 中通过点击和端点登录。
  2. 一个对象名 userDao 从数据库中获取用户,returns 一个 User 具有 Bill 数组的对象。 UserDaoUser 都在 server2.js 的顶部 required。
  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。

当您在 session 中存储一个对象时,它被序列化为 JSON。当您检索它时,它会再次被解析为一个普通对象。因此,任何原始原型信息都会在此过程中丢失。正如 express-session module:

的文档中所述

To store or access session data, simply use the request property req.session, which is (generally) serialized as JSON by the store [...]

要恢复原型信息,您可以这样做:

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]);