0

変数に少し問題があります。

関数の戻り時にオブジェクト (ObjectQueue) を構築したいのですが、コールバック関数の要求で初期化された変数に同じスコープがなく、解決策が見つからないため、できません。

変数リソース、nameUser および JsonObject は未定義です。解決策はありますか?

Plazza.ts

const request = require('request');
import { ObjectQueue } from '../ObjectQueue';
import { QueueService } from '../QueueService';

export class Plazza extends QueueService {

constructor() {
super('plazza');
}

getInfoFromService(username, password, url?, nameFile?): ObjectQueue {
console.log('username : ' + username);
console.log('password : ' + password);

let urlUsing = 'https://****.com/api/core/v3/people/email/' + username;
let nameFileUsing = 'user';

let resources: String[];
let nameUser:String;
let jsonObject:any;

if (url) {
  urlUsing = url;
}

if (nameFile) {
  nameFileUsing = nameFile;
}

const optionsUser = {
  url: urlUsing,
  auth: {
    password,
    user: username,
  },
  method: 'GET',
  headers: {
    'Content-Type': 'application/json',
    'user-agent': 'node.js', 'Allow-Control-Allow-Origin': '*',
  },
};

request(optionsUser, requestPlazza);

function requestPlazza(err, res, body) {
  if (err) {
    console.dir(err);
    return;
  }

  console.log('\nstatus code of ' + urlUsing + ' :', res && res.statusCode + '\n');

  jsonObject = JSON.parse(body);

  nameUser = jsonObject.name.familyName + '-' + jsonObject.name.givenName;

  const ressource = jsonObject.resources;
  for (const key in ressource) {
    if (ressource.hasOwnProperty(key)) {
      if (key === 'activity' || key === 'members') {
        console.log(key + ' : ' + ressource[key].ref);
        resources.push(ressource[key].ref);
      }
    }
  }
}
return new ObjectQueue(nameUser, this.nameService, nameFileUsing + '.json', jsonObject, resources);
}
}

ObjectQueue.ts

export class ObjectQueue {

public nameUser: string;
public nameService: string;
public nameFile: string;
public jsonObject: any;
public resources: string[];

 constructor(nameUser: string, nameService: string, nameFile: string, jsonObject: any, resources: string[]) {
   this.nameUser = nameUser;
   this.nameService = nameService;
   this.nameFile = nameFile;
   this.jsonObject = jsonObject;
   this.resources = resources;
 }
}
4

0 に答える 0