如何从javascript中的回调响应数组中检索值

时间:2017-12-06 12:34:19

标签: javascript arrays

我尝试从google drive uploader的回调响应中获取id文件。我使用的代码是这样的:

function handleFileSelect(evt) {
           document.getElementById("info_upload").innerHTML = "Sedang memproses file untuk diupload...";
         evt.stopPropagation();
         evt.preventDefault();
         var files = evt.dataTransfer.files; // FileList object.

         var output = [];
         for (var i = 0, f; f = files[i]; i++) {
             var uploader = new MediaUploader({
                 file: f,
                 token: accessToken,
                 onComplete: function(data) {
                    document.getElementById('result').value = data['id'];
                 }
             });
             uploader.upload();
         }
       }

我已经尝试了所有我知道的方法来获取id文件,但它仍然无法正常工作。我已经尝试过data.id或data [数组索引]。

这是Callback响应数组的全部值。

{
 "kind": "drive#file",
 "id": "1_EfEFzN4qppKGrcDK6T_SS5hdeAovFK-",
 "etag": "\"rOX0fJzgyZU36BYOEcGKIYxPCtI/MTUxMjU1MzQwMzA0Mw\"",
 "selfLink": "https://www.googleapis.com/drive/v2/files/1_EfEFzN4qppKGrcDK6T_SS5hdeAovFK-",
 "webContentLink": "https://drive.google.com/uc?id=1_EfEFzN4qppKGrcDK6T_SS5hdeAovFK-&export=download",
 "alternateLink": "https://drive.google.com/file/d/1_EfEFzN4qppKGrcDK6T_SS5hdeAovFK-/view?usp=drivesdk",
 "embedLink": "https://drive.google.com/file/d/1_EfEFzN4qppKGrcDK6T_SS5hdeAovFK-/preview?usp=drivesdk",
 "iconLink": "https://drive-thirdparty.googleusercontent.com/16/type/image/png",
 "thumbnailLink": "https://lh5.googleusercontent.com/D_wWrHgdhRw6fnLaG0w2_nczIHxP1_gzD1-0N47_nlLZk2dXCLPP1GPj6SN37TQHNvvsG54zJSQ=s220",
 "title": "1.png",
 "mimeType": "image/png",
 "labels": {
  "starred": false,
  "hidden": false,
  "trashed": false,
  "restricted": false,
  "viewed": true
 },
 "createdDate": "2017-12-06T09:43:23.043Z",
 "modifiedDate": "2017-12-06T09:43:23.043Z",
 "modifiedByMeDate": "2017-12-06T09:43:23.043Z",
 "lastViewedByMeDate": "2017-12-06T09:43:23.043Z",
 "markedViewedByMeDate": "1970-01-01T00:00:00.000Z",
 "version": "2",
 "parents": [
  {
   "kind": "drive#parentReference",
   "id": "0ALL4ishr4YNbUk9PVA",
   "selfLink": "https://www.googleapis.com/drive/v2/files/1_EfEFzN4qppKGrcDK6T_SS5hdeAovFK-/parents/0ALL4ishr4YNbUk9PVA",
   "parentLink": "https://www.googleapis.com/drive/v2/files/0ALL4ishr4YNbUk9PVA",
   "isRoot": true
  }
 ],
 "downloadUrl": "https://doc-04-28-docs.googleusercontent.com/docs/securesc/faqhpav83cnunr53av2ik9r00tmicagg/7fvl938cmksp83dk43pp0oo5qiseh3sr/1512547200000/13075436128251877983/13075436128251877983/1_EfEFzN4qppKGrcDK6T_SS5hdeAovFK-?e=download&gd=true",
 "userPermission": {
  "kind": "drive#permission",
  "etag": "\"rOX0fJzgyZU36BYOEcGKIYxPCtI/ZgAdmVNnouRC2sSB3efF5W7Fobk\"",
  "id": "me",
  "selfLink": "https://www.googleapis.com/drive/v2/files/1_EfEFzN4qppKGrcDK6T_SS5hdeAovFK-/permissions/me",
  "role": "owner",
  "type": "user"
 },
 "originalFilename": "1.png",
 "fileExtension": "png",
 "md5Checksum": "dd9b4aa37a7a35de368d73d002ba6bf9",
 "fileSize": "852232",
 "quotaBytesUsed": "852232",
 "ownerNames": [
  "Abdul Kahar"
 ],
 "owners": [
  {
   "kind": "drive#user",
   "displayName": "Abdul Kahar",
   "picture": {
    "url": "https://lh5.googleusercontent.com/-vsm5aHKmIJU/AAAAAAAAAAI/AAAAAAAAAY8/KIbhdHI1by4/s64/photo.jpg"
   },
   "isAuthenticatedUser": true,
   "permissionId": "13075436128251877983",
   "emailAddress": "abdulkaharumar@gmail.com"
  }
 ],
 "lastModifyingUserName": "Abdul Kahar",
 "lastModifyingUser": {
  "kind": "drive#user",
  "displayName": "Abdul Kahar",
  "picture": {
   "url": "https://lh5.googleusercontent.com/-vsm5aHKmIJU/AAAAAAAAAAI/AAAAAAAAAY8/KIbhdHI1by4/s64/photo.jpg"
  },
  "isAuthenticatedUser": true,
  "permissionId": "13075436128251877983",
  "emailAddress": "abdulkaharumar@gmail.com"
 },
 "capabilities": {
  "canCopy": true,
  "canEdit": true
 },
 "editable": true,
 "copyable": true,
 "writersCanShare": true,
 "shared": false,
 "explicitlyTrashed": false,
 "appDataContents": false,
 "headRevisionId": "0B7L4ishr4YNbRDRYRXZhVUJlM2VtSEh2ai9JVlR3NnFZbkJFPQ",
 "imageMediaMetadata": {
  "width": 1365,
  "height": 719,
  "rotation": 0
 },
 "spaces": [
  "drive"
 ]
}

如果我使用数据[0]那么只显示“{”,其他如数据[1],数据[2]和数据[3]则不起作用。

1 个答案:

答案 0 :(得分:1)

看起来您需要反序列化JSON响应以将其从字符串转换为对象。

import { Component, ViewChild } from '@angular/core';
import { IonicPage, NavController, NavParams,AlertController } from 'ionic-angular';
import { AngularFireAuth } from 'angularfire2/auth';
import { AngularFireDatabase} from 'angularfire2/database';

/**
 * Generated class for the LaporanharianPage page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */

@Component({
  selector: 'page-laporanharian',
  templateUrl: 'laporanharian.html',
})
export class LaporanharianPage {


  myDate: String = new Date().toISOString();
  id : string;

  constructor(private alertctrl :AlertController ,private db : AngularFireDatabase,public fire :AngularFireAuth,public navCtrl: NavController, public navParams: NavParams) {
  }

  @ViewChild('mydate') mydate;
  @ViewChild('porsiKarbohidrat') porsiKarbohidrat;
  @ViewChild('porsiProteinHewani') porsiProteinHewani;
  @ViewChild('porsiProteinNabati') porsiProteinNabati;
  @ViewChild('porsiLemak') porsiLemak;

  ionViewDidLoad() {
    console.log('ionViewDidLoad LaporanharianPage');
  }
  alert(message : string)
  {
    this.alertctrl.create({
      title: 'info !',
      subTitle: message,
      buttons: ['OK']
    }).present();
  }

  submitLaporan()
  {

    this.id = this.fire.auth.currentUser.uid;
    this.db.list('/laporan/'+this.id).push({
      mydate : this.mydate.value,
      porsiKarbohidrat : this.porsiKarbohidrat.value,
      porsiProteinHewani : this.porsiProteinHewani.value,
      porsiProteinNabati : this.porsiProteinNabati.value,
      porsiLemak : this.porsiLemak.value,
    })

    this.alert("Sukses")
    }



}