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

Error on getStatus of VideoEncodingStatusChecker

Open spicy-sauce opened this issue 4 years ago • 7 comments

Which SDK version are you using?

SDK 7.0

What's the issue?

it is required an await on api.call inside of getStatus method from VideoEncodingStatusChecker

static getStatus(api: FacebookAdsApi, videoId: number) { const result = api.call('GET', [videoId.toString()], {fields: 'status'}); // $FlowFixMe return result['status']; } result is a Promise.

Steps/Sample code to reproduce the issue

const adVideo = new AdVideo( null, { [AdVideo.Fields.filepath]: filepath }, this._account._data.id );

    const videoResponse = await adVideo.create();
    adVideo[AdVideo.Fields.id] = videoResponse.id;
    await adVideo.waitUntilEncodingReady();

Observed Results:

  • What happened? This could be a description, log output, etc. UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'video_status' of undefined

Expected Results:

  • What did you expect to happen? static getStatus(api: FacebookAdsApi, videoId: number) { const result = await api.call('GET', [videoId.toString()], {fields: 'status'}); // $FlowFixMe return result['status']; }

Same as issue#160

spicy-sauce avatar Aug 30 '20 18:08 spicy-sauce

Anyone?

spicy-sauce avatar Sep 22 '20 05:09 spicy-sauce

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]

Still requires the community's attention

spicy-sauce avatar Dec 26 '20 17:12 spicy-sauce

I am experiencing the same issue

ymulenll avatar Jan 16 '21 12:01 ymulenll

I solved that problem like this.

I added getStatus function in my code

FacebookSDK.prototype.getStatus = function (videoId, params) { return this._api.call("GET", [videoId.toString()], params); };

After that in time upload video I change my code to this

const adVideoInstance = sdk.instance(AdVideo, {
    parentId: adAccount.id,
    data: {  'filepath': videoData.file } });

const adVideo = await adVideoInstance.create();

const sleepTime = 10; // seconds
const processTimeout = 900; // seconds

let elapsedTime = 0;
const cond = true;

/** Video creating process */
while (cond) {
    if (elapsedTime >= processTimeout) {
        throw new Error("Video file processing timeout.");
    }

    await sleep(sleepTime);
    elapsedTime += sleepTime;

   // Use my own getStatus function
    const adVideoLoaded = await sdk.getStatus(adVideo.id, { fields: "status" });

    if (adVideoLoaded.status.video_status == "processing") {
        console.log("Ad-video status still equal to 'processing'.");
        continue;
    } else if (adVideoLoaded.status.video_status == "ready") {
        return adVideo;
    } else {
        throw new Error("Video upload error");
    }
}

Edo777 avatar Sep 09 '21 08:09 Edo777

I solved that problem like this.

I added getStatus function in my code

FacebookSDK.prototype.getStatus = function (videoId, params) { return this._api.call("GET", [videoId.toString()], params); };

After that in time upload video I change my code to this

const adVideoInstance = sdk.instance(AdVideo, {
    parentId: adAccount.id,
    data: {  'filepath': videoData.file } });

const adVideo = await adVideoInstance.create();

const sleepTime = 10; // seconds
const processTimeout = 900; // seconds

let elapsedTime = 0;
const cond = true;

/** Video creating process */
while (cond) {
    if (elapsedTime >= processTimeout) {
        throw new Error("Video file processing timeout.");
    }

    await sleep(sleepTime);
    elapsedTime += sleepTime;

   // Use my own getStatus function
    const adVideoLoaded = await sdk.getStatus(adVideo.id, { fields: "status" });

    if (adVideoLoaded.status.video_status == "processing") {
        console.log("Ad-video status still equal to 'processing'.");
        continue;
    } else if (adVideoLoaded.status.video_status == "ready") {
        return adVideo;
    } else {
        throw new Error("Video upload error");
    }
}

Hi, Could you please provide a full example ? I can't manage to find what is sdk, since instance does not seem be to working on the facebook-nodejs-business-sdk'.

Thanks!

Status

grallc avatar Oct 06 '21 16:10 grallc

Hi @grallc. Here is an example of my code, but you need to change some variables and set your settings.

FacebookBusinessSDK.js

module.exports = require("facebook-nodejs-business-sdk");

FacebookSDK.js

const FacebookBusinessSDK = require("./FacebookBusinessSDK");

const FacebookSDK = function (appId, appSecret, accessToken) {
    this._appId = appId;
    this._appSecret = appSecret;

    this._api = new FacebookBusinessSDK.FacebookAdsApi(accessToken, "en_US");
    const defaultApi = FacebookBusinessSDK.FacebookAdsApi.getDefaultApi();

    if (!defaultApi) {
        FacebookBusinessSDK.FacebookAdsApi.setDefaultApi(this._api);
    }
};

FacebookSDK.prototype.instance = function (instanceType, args = {}) {
    return new instanceType(args.id || null, args.data || {}, args.parentId || null, this._api);
};

FacebookSDK.prototype.getResource = function (resourceId, params) {
    return this._api.call("GET", [resourceId.toString()], params);
};

FacebookBusinessSDK.FacebookSDK = FacebookSDK;
module.exports = FacebookBusinessSDK;

Create-Ad.js

const {AdAccount, AdVideo, FacebookSDK} = require("./FacebookSDK.js");

const sdk = new FacebookSDK(
      YOUR_FACEBOOK_APP_ID,
      YOUR_FACEBOOK_APP_SECRET,
      YOUR_FACEBOOK_ACCESS_TOKEN
);

async function createVideoAd(videoFilePath){
      const adVideoInstance = sdk.instance(AdVideo, {
          parentId: YOUR_FACEBOOK_AD_ACCOUNT_ID,
          data: {
              [AdVideo.Fields.filepath]: videoFilePath,
          },
      });
      
      const adVideo = await adVideoInstance.create();
      
      const sleepTime = 10; // seconds
      const processTimeout = 900; // seconds
      
      let elapsedTime = 0;
      const cond = true;
      
       /** Video creating process */
      while (cond) {
          if (elapsedTime >= processTimeout) {
              throw new Error("Video file processing timeout.");
          }
      
          await sleep(sleepTime);
          elapsedTime += sleepTime;
      
          const adVideoLoaded = await sdk.getResource(adVideo.id, { fields: "status" });
      
          if (adVideoLoaded.status.video_status == "processing") {
              console.log("Ad-video status still equal to 'processing'.");
              continue;
          } else if (adVideoLoaded.status.video_status == "ready") {
              return adVideo;
          } else {
              throw new Error("Video upload error");
          }
      }
      
      throw new Error("Unknown error in time video upload");
}

module.exports = {createVideoAd}

Thanks!

Edo777 avatar Oct 26 '21 14:10 Edo777