Javascript 为什么我会遇到这样的错误:“;time.replaceAll不是一个函数;
您好,我一直收到一个错误“time.replaceAll不是函数”,我不明白为什么,因为我在一个在线代码编辑器中尝试了Javascript 为什么我会遇到这样的错误:“;time.replaceAll不是一个函数;,javascript,node.js,express,Javascript,Node.js,Express,您好,我一直收到一个错误“time.replaceAll不是函数”,我不明白为什么,因为我在一个在线代码编辑器中尝试了time.replaceAll,它成功了 exports.ForgotPasswordToken = async (req, res) => { username = req.body.username; const add_minutes = function (dt, minutes) { return new Date(dt.getTime() + m
time.replaceAll
,它成功了
exports.ForgotPasswordToken = async (req, res) => {
username = req.body.username;
const add_minutes = function (dt, minutes) {
return new Date(dt.getTime() + minutes * 60000);
};
const time = add_minutes(new Date(), 10).toString();
const decimalTime = time.replaceAll(":", "").substr("16", "6");
const tokenExp = decimalTime;
const token = Math.random()
.toString(24)
.replace(/[^a-z]+/g, "")
.substr(0, 5);
hashed_token = bcrypt.genSalt(10, (err, salt) => {
bcrypt.hash(token, salt, (err, hash) => {
if (err) {
console.log(err);
}
});
});
await User.findOne({ username: username }, (err, obj) => {
const userEmail = obj.email;
});
let newToken = new Token({
tokenUser: username,
tokenVal: hashed_token,
tokenExpiration: tokenExp,
});
const resetLink = `https://localhost:5000/password/reset/${token}/${username}`;
newToken.save();
passwordResetEmail(username, userEmail, resetLink);
res.redirect("/login");
};
String.prototype.replaceAll
是JS环境中提供的一项非常新的功能。如果要在节点中使用它,请升级到节点15(15.0.0是支持它的最早节点版本),或者改用正则表达式:
time.replace(/:/g, '')
谢谢,我明白了