Questo issue nel repository SuperAgent menziona il metodo .use
per aggiungere la logica su ogni richiesta. Ad esempio, l'aggiunta di un Authorization
intestazione per JWT quando un token è disponibile:Metodo ".use" per aggiungere la logica su ciascuna richiesta di SuperAgent
superagent.use(bearer);
function bearer (request) {
var token = sessionStorage.get('token');
if (token) request.set('Authorization', 'Bearer ' + token);
}
Anche se l'ultimo commento informa che questa funzione è lavorare di nuovo, non posso farlo funzionare.
Il seguente codice di prova:
var request = require('superagent');
request.use(bearer);
function bearer (request)
{
// "config" is a global var where token and other stuff resides
if (config.token) request.set('Authorization', 'Bearer ' + config.token);
}
restituisce questo errore:
request.use(bearer);
^
TypeError: undefined is not a function
Per favore, spiegate i motivi per cui avete downvoted la domanda: http://meta.stackexchange.com/questions/135/encouraging-people-to-explain-downvotes –
la funzione 'request' che viene esportata da superagent non avere un metodo 'use'. tuttavia, l'istanza di 'Request' che viene restituita eseguendo' request'. Non è chiaro se questo sia un bug o possa essere risolto senza una richiesta di pull. –
In altre parole, l'errore che si sta ottenendo è previsto, in base alla visualizzazione dell'origine. Il readme inoltre non mostra un esempio del tuo utilizzo, quindi (il tuo caso d'uso) potrebbe non essere un uso previsto. –