gpt4 book ai didi

javascript - 无法使用快速中间件创建 cookie

转载 作者:太空宇宙 更新时间:2023-11-04 02:01:58 25 4
gpt4 key购买 nike

每次新用户注册我的应用程序时,我都会尝试创建一个 cookie,表明这是他们第一次登录。我找到了this thread并粗略地遵循了它,但它不起作用。

这是我当前的代码:

// setting up the middle ware
export default function () {
return function () {
const app = this
app.use(cookieParser())
app.post('/app/signup/end', [firstTimeCookie(app), signUp(app)])
}
}

不工作的中间件是 firstTimeCookie()signUp() 工作正常。

// in firstTimeCookie:

export default function (app) {
return function (req, res, next) {
const cname = 'FIRST_SIGNUP'
const cvalue = true
var cookie = req.cookies.cookieName

if (!cookie) {
res.cookie(cname, cvalue, { maxAge: 1000 * 60 * 60 * 24 * 999999 })
} else {
console.log('cookie already exists')
}
next()
}
}

我做错了什么?

最佳答案

问题是 cookie未定义

更改:

var cookie = req.cookies.cookieName

对于:

var cookie = req.cookies['FIRST_SIGNUP'];

然后你就可以处理cookie了:

if (!cookie) {
console.log('Cookie not there');
resp.cookie('FIRST_SIGNUP', 'Hello World', { maxAge: 1000 * 60 * 60 * 24 * 999999 });
} else {
console.log(cookie);
console.log('cookie already exists');
}

next();

关于javascript - 无法使用快速中间件创建 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45651335/

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