node-linkedin icon indicating copy to clipboard operation
node-linkedin copied to clipboard

res.redirect is not a function

Open ctfrancia opened this issue 4 years ago • 1 comments

I have a sandbox app with linkedin however, I keep getting the issue res.redirect is not a function not sure where the error lies.

import { Context } from 'vm';
const Linkedin = require('node-linkedin')('client_id', 'client_secret', 'callback_url');
const linkedin = Linkedin.init('client_id');

export const auth = (ctx: Context): any => {
  const scope = 'r_liteprofile';
  const info = Linkedin.auth.authorize(scope);
  console.log('res', info);
};

ctfrancia avatar Sep 02 '19 12:09 ctfrancia

I got the same error. I looked into the code which caused the error, which is in node_modules/node-linkedin/lib/auth.js then at line 32, I see the below code.

this.authorize = function (res, scope, state, redirectURI) {
  if (res && res.constructor === Array) {
    redirectURI = state;
    state = scope;
    scope = res;
    res = null;
  }

and I thought this code is trying to make the first parameter set to scope when the first value corresponds to the type of scope(i.e. string). (by default the 'authorize' method takes the first parameter as 'res') So I feel weird that the part res.constructor === Array should be res.constructor === String not Array then I changed the part.

this.authorize = function (res, scope, state, redirectURI) {
  if (res && res.constructor === String) {
    redirectURI = state;
    state = scope;
    scope = res;
    res = null;
  }

Also, there was a weird code on line 55,

if (scope && scope.length > 0) {
   url += '&scope=' + scope.join('%20');
}

the variable 'scope' should be string, not array. so .join('%20') is weird as well. so I commented out the part.

if (scope && scope.length > 0) {
   url += '&scope=' + scope/*.join('%20')*/;
}

then the error is gone. I am new to Linkedin API and I don't know much about this library. I am not sure if this is what I'm supposed to do for the error but I hope it helped. Sorry for my bad English if it is hard to read.

Momijiichigo avatar Aug 04 '20 05:08 Momijiichigo