nodejs cookie-session typeerror

1.4k views Asked by At

TypeError: this.keys.index is not a function

the console log show that the if condition line is error

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

it was fine until i start another server.js from a completely different folder. I have tried to reinstall the node_module to fix it but it just result the same. but the weird part is i can start the server in a vm without any problem. can any help me with this?

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
  }
};
1

There are 1 answers

0
Geoff On

I realise that this answer is late in the day but might help someone with the same error. I was getting the error 'TypeError: this.keys.index is not a function' on line 73 of index.js in cookies when trying to use koa-session.

I eventually realised that I had set app.keys to a single value i.e.

app.keys = 'dieueyf7huienejnfef'

When it should be an array:

app.keys = ['dieueyf7huienejnfef']