facebook-nodejs-business-sdk icon indicating copy to clipboard operation
facebook-nodejs-business-sdk copied to clipboard

TypeError: response.data.map is not a function with getReachEstimate

Open jneff02 opened this issue 5 years ago • 19 comments

Which SDK version are you using?

3.3.1

What's the issue?

When using getReachEstimate for ad accounts, I am receiving the error 'TypeError: response.data.map is not a function'. It doesn't matter what parameters I put in, I keep getting this error. I've even stripped down my parameters to a very simple test and get the same results. If I enable debugging it shows a response of 'Response: {"data":{"users":27000,"estimate_ready":true}}' immediately followed by the TypeError. At this point, I am not doing anything other than console logging the returned promise.

Steps/Sample code to reproduce the issue

const fields = []
const params = {
    targeting_spec: {
      geo_locations: {
        countries: ["US"]
      }
    }
  }
const getReach = await adAccount.getReachEstimate(fields, params)

Observed Results:

  • What happened? This could be a description, log output, etc. Stack Trace:
TypeError: response.data.map is not a function
    at Array.Cursor._this._buildObjectsFromResponse (.../node_modules/facebook-nodejs-business-sdk/dist/cjs.js:1318:28)
    at .../node_modules/facebook-nodejs-business-sdk/dist/cjs.js:1306:31
    at bound (domain.js:370:14)
    at runBound (domain.js:383:12)
    at tryCatcher (.../node_modules/bluebird/js/release/util.js:16:23)
    at Promise._settlePromiseFromHandler (.../node_modules/bluebird/js/release/promise.js:512:31)
    at Promise._settlePromise (.../node_modules/bluebird/js/release/promise.js:569:18)
    at Promise._settlePromise0 (.../node_modules/bluebird/js/release/promise.js:614:10)
    at Promise._settlePromises (.../node_modules/bluebird/js/release/promise.js:694:18)
    at _drainQueueStep (.../node_modules/bluebird/js/release/async.js:138:12)
    at _drainQueue (.../node_modules/bluebird/js/release/async.js:131:9)
    at Async._drainQueues (.../node_modules/bluebird/js/release/async.js:147:5)
    at Immediate.Async.drainQueues [as _onImmediate] (.../node_modules/bluebird/js/release/async.js:17:14)
    at runCallback (timers.js:756:18)
    at tryOnImmediate (timers.js:717:5)
    at processImmediate [as _immediateCallback] (timers.js:697:5)

jneff02 avatar May 16 '19 02:05 jneff02

So it appears to be the case that despite Facebook's documentation (https://developers.facebook.com/docs/marketing-api/reference/ad-account/reachestimate/) which claims that "data" comes back as an array, it comes back like this:

{"data":{"users":1500000,"estimate_ready":true}}

which, then in this sdk, gets used like this: https://github.com/facebook/facebook-nodejs-business-sdk/blob/bcfc657bc3b16d228b1887d34b8c75b937a91050/src/cursor.js#L103

So, thus the error of response.data.map being undefined - it definitely is because response.data is an object and not an array.

It's unclear to me exactly how to fix it but I'd be more than happy to submit a PR to do so. I guess I'd bet that the right thing to do here is to check if response.data is an Array before calling map, and otherwise just assume it's a single instance of the target class that this method is trying to turn it into.

dannyjlaurence-exp avatar Oct 17 '19 17:10 dannyjlaurence-exp

I'm having the same issue. I'm not very familiar with this library yet, but it looks like the issue has to do with the "fetchFirstPage" boolean in the "getEdge". It seems to me that the promise is expecting pagination as part of the response even if it is unavailable. Again, my issue is similar, and I'm just getting familiar with this.

dcordell26 avatar Nov 19 '19 19:11 dcordell26

Hello. Same issue here too. @dcordell26, I'm in a process to get familiar with this sdk too. Is there a way to discuss with you to see how we can help each other ? Can you reach me on my email (on my profil )?

goffle avatar Nov 20 '19 06:11 goffle

I ran across this same error. The comment above by dannyjlaurence-exp helped point me in the right direction. I created a quick fix for this issue until it is fixed more permanently. In my fixed file, the code starting on line 102 of src/cursor.js looks like this:

this._buildObjectsFromResponse = response => {
      if (typeof response.data === 'object') {
        let That: any = this._targetClass;
        const result = new That(
          response.data && response.id ? response.id : null,
          response.data,
          undefined,
          this._api,
        );
        const returnResponse = [result];
        return returnResponse;
      } else {
        return response.data.map(item => {
          let That: any = this._targetClass;
          if (That.name === 'AbstractObject') {
            var result = new That();
            result.setData(item);
            return result;
          }
          return new That(
            item && item.id ? item.id : null,
            item,
            undefined,
            this._api,
          );
        });
      }
    };

This is a quick fix for this until it is fixed permanently.

lrdueck avatar Dec 05 '19 16:12 lrdueck

Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Mar 04 '20 17:03 stale[bot]

It requires approval by a Facebook dev. It is still a problem.

dannyjlaurence-exp avatar Mar 04 '20 17:03 dannyjlaurence-exp

This is still an issue. Is the only workaround hitting the API directly ?

royzer avatar Mar 11 '20 13:03 royzer

Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Jun 09 '20 13:06 stale[bot]

As far as I know, this issue has not been fixed.

dannyjlaurence-exp avatar Jun 09 '20 14:06 dannyjlaurence-exp

I think the code should check the 'response.data' is not an array. this._buildObjectsFromResponse = response => { if (typeof response.data === 'object' && !Array.isArray(response.data)) { let That: any = this._targetClass; const result = new That( response.data && response.id ? response.id : null, response.data, undefined, this._api, ); const returnResponse = [result]; return returnResponse; } else { return response.data.map(item => { let That: any = this._targetClass; if (That.name === 'AbstractObject') { var result = new That(); result.setData(item); return result; } return new That( item && item.id ? item.id : null, item, undefined, this._api, ); }); } };

TallulahYang avatar Aug 07 '20 08:08 TallulahYang

This is still an issue. even with latest SDK versions :(

aaronhayes avatar Sep 15 '20 04:09 aaronhayes

My offer still stands in that I'ld submit a PR fixing this, but as per usual with Facebook it's very unclear where or how to do so. I believe these SDK's are generated so the fix is likely upstream.

dannyjlaurence-exp avatar Sep 15 '20 12:09 dannyjlaurence-exp

Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Dec 25 '20 13:12 stale[bot]

As far as I know, this issue has not been fixed. See my comments above.

dannyjlaurence-exp avatar Dec 25 '20 17:12 dannyjlaurence-exp

Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Jan 09 '22 02:01 stale[bot]

As far as I know, this issue has not been fixed. See my comments above.

dannyjlaurence-exp avatar Jan 10 '22 13:01 dannyjlaurence-exp

Unreal. About to be the four year anniversary of this issue. Still not fixed.

johncmunson avatar May 11 '23 06:05 johncmunson

We have abandoned the SDK and have been going direct to GraphAPI. The SDK is just too buggy.

lrdueck avatar May 11 '23 13:05 lrdueck