gpt4 book ai didi

javascript - ES6 : "TyperError: X is not a function"

转载 作者:行者123 更新时间:2023-12-02 23:52:57 25 4
gpt4 key购买 nike

我正在尝试向 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 从数据库中获取用户,并返回一个包含 Bill 数组的 User 对象。 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。

最佳答案

当您将对象存储在session中时,它会被序列化为JSON。当您检索它时,它会被再次解析为普通对象。因此,任何原始原型(prototype)信息都会在此过程中丢失。如 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 [...]

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

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

关于javascript - ES6 : "TyperError: X is not a function",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55562001/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com