2

I am trying to retrieve values from a table in quickbase. image of quickbase table

this is what i have so far.... service.ts

    public getNativeFields(){
        let headers = new HttpHeaders().set("QB-Realm-Hostname", "xxxxxx.quickbase.com").set("Authorization", "QB-USER-TOKEN xxxxxx_xxx_xxxxxxxxxxxxxxxxxxxxxxxxx");
    
        let requestBody = {
          "from": this.someTableTableId,
          "select": [6,7]
        }
        return this.httpClient.post(`${this.apiURL}/records/query?appId=${this.appId}`, requestBody, { headers })
 
    }

in my component.ts I have...

    ngOnInit(): void {
        let list= this.apiService.getNativeFields();
        console.log("list is" );
        console.log(list);
        list.forEach(element=>{
          this.nativeVersions.push(element);
        })
        console.log("native versions");
        console.log(this.nativeVersions);
      }

this log returns

image of array of object that was logged into console

how do I extract the {value:"1.0.0"} and {value:"1.0.5"}

Owen Kelvin
  • 14,054
  • 10
  • 41
  • 74
Hugh
  • 19
  • 5
  • Hi @Kevner, the point is that `this.apiService.getNativeFields()` will return a observable to you. Try to do it: `this.apiService.getNativeFields().subscribe((response) => { console.log(response); // Verify if its return the same on your print response[0].data.[0][7] // {value:"1.0.0"} response[0].data.[0][7] // {value:"1.0.5"} })` – Patrick Oct 28 '20 at 09:11
  • @Patrick ahh yes youre right. So subscribing returns these arrays `{data: Array(2), fields: Array(2), metadata: {…}} data: Array(2) 0: 6: {value: "1.0.0"} 7: {value: "

    Test

    ↵ ↵
    • test 1
    • test 2
    • test 3
    ↵"} __proto__: Object 1: 6: {value: "1.0.5"} 7: {value: "

    test

    ↵ ↵
    • test
    ↵ ↵

    test

    ↵ ↵
    • test
    ↵"} __proto__: Object length: 2 __proto__: Array(0) fields: (2) [{…}, {…}] metadata: {numFields: 2, numRecords: 2, skip: 0, totalRecords: 2} __proto__: Object`
    – Hugh Oct 28 '20 at 14:56
  • @Patrick but, `response[0].data.[0][7]` still gives me that error `core.js:6241 ERROR TypeError: Cannot read property 'data' of undefined at SafeSubscriber._next (current-versions.component.ts:39) at SafeSubscriber.__tryOrUnsub (Subscriber.js:183) at SafeSubscriber.next (Subscriber.js:122) at Subscriber._next (Subscriber.js:72) **Thanks for all the help so far @Patrick. I appreciate it :) – Hugh Oct 28 '20 at 14:57
  • According to your return, it should be available here: `response.data[0][6]`. – Patrick Oct 28 '20 at 15:16
  • @Patrick yea, that didnt work... but this did.. `response["data"][0][6]` Thanks again, though. The help was greatly appreciated – Hugh Oct 28 '20 at 17:18

2 Answers2

0

It should work to retrieve values:

this.nativeVersions[0].data[0]["6"] //{value:"1.0.0"}
this.nativeVersions[0].data[1]["6"] //{value:"1.0.5"}
Patrick
  • 103
  • 11
  • Hey @Patrick ! thanks for the response. I tried what you suggested but i get this error `ERROR TypeError: Cannot read property 'data' of undefined at CurrentVersionsComponent.ngOnInit (current-versions.component.ts:35) at callHook (core.js:4726) at callHooks (core.js:4690) at executeInitAndCheckHooks (core.js:4630) at refreshView (core.js:12026) at refreshEmbeddedViews (core.js:13404) at refreshView (core.js:12035) at refreshComponent (core.js:13458) at refreshChildComponents (core.js:11729) at refreshView (core.js:12064)` – Hugh Oct 28 '20 at 03:25
0

Try a dynamic generation of the fields, this way you wont have to worry about which index was for version of notes

From the Object, we can see we have

[
  {
     data: [...],
     fields: [...]
  }
]

The fields define how the data structure is, so we can generate the a simpler object through the map operator. See below code

  constructor(private apiService: ApiService) {}
  list$ = this.apiService.getNativeFields();
  allVersions$ = this.list$.pipe(
    map(list =>
      (list.map(({ fields, data }) =>
        data.map(item =>
          fields.reduce((prev, next) => {
            return { ...prev, [next.label]: item[next.id]["value"] };
          }, {})
        )
      ) as any).flat()
    ),
    map(item => item.map(({ version }) => version))
  );
  ngOnInit() {
    this.allVersions$.subscribe({
      next: items => console.log(items)
    });
  }

See this demo on stackblitz

NB .flat() function is only available in es2019. This post will help with any challenges Typescript flatMap, flat, flatten doesn't exist on type any[]

Owen Kelvin
  • 14,054
  • 10
  • 41
  • 74