0

TypeError:this.keys.index 不是函数

控制台日志显示 if 条件行是错误的

app.get('/',(req,res)=>{
    if(!req.session.authenticated){
        res.redirect('/login');
    }else{
        res.redirect('/search')
    }
})

直到我从一个完全不同的文件夹启动另一个 server.js 之前都很好。我试图重新安装 node_module 来修复它,但结果是一样的。但奇怪的是我可以毫无问题地在 vm 中启动服务器。有什么可以帮助我的吗?

Cookies.prototype.get = function(name, opts) {
  var sigName = name + ".sig"
    , header, match, value, remote, data, index
    , signed = opts && opts.signed !== undefined ? opts.signed : !!this.keys

  header = this.request.headers["cookie"]
  if (!header) return

  match = header.match(getPattern(name))
  if (!match) return

  value = match[1]
  if (!opts || !signed) return value

  remote = this.get(sigName)
  if (!remote) return

  data = name + "=" + value
  if (!this.keys) throw new Error('.keys required for signed cookies');
 index = this.keys.index(data, remote)

  if (index < 0) {
    this.set(sigName, null, {path: "/", signed: false })
  } else {
    index && this.set(sigName, this.keys.sign(data), { signed: false })
    return value
  }
};
4

1 回答 1

1

我意识到这个答案已经很晚了,但可能会帮助有同样错误的人。尝试使用 koa-session 时,我在 cookie 中 index.js 的第 73 行收到错误“TypeError:this.keys.index is not a function”。

我最终意识到我已将 app.keys 设置为单个值,即

app.keys = 'dieueyf7huienejnfef'

当它应该是一个数组时:

app.keys = ['dieueyf7huienejnfef']
于 2021-11-23T19:09:34.480 回答