How can we pass the data on update/create to third party api to update records

Hi Team,

I need help to understand how can I disable the default update feature of forest admin. I need to update record using the third party call. below is my code snippt.

Code snippt


// Update a Adminsetting
router.put('/adminsettings/:recordId', permissionMiddlewareCreator.update(), (request, response, next) => {
  const RecordUpdater = new RecordUpdater('adminsettings');
  const data = request.body.data;
  axios({
      url: 'https://example.com/api/users',
      method: 'post',
      data: data,
    })
    .then(async (result) => {
      // console.log(result);
      response.send(await RecordUpdaterM.serialize(result.data));
    })
    .catch((error) => {
      console.log('error:', error);
    });
    next();
});

Actual behavior

Here I need to pass the image object and other relational data ID only to external API to update. Here is the external API is app API associate with the same database as Mongo DB.

  • Express Version: 4.17.1
  • mongoos Version: 5.8.2
  • Database Dialect: mongodb
  • Axios: 0.21.1

Hello @sourabht,

Thanks for reaching out :raised_hands:

Did you check this documentation?
I guess that’s what you need to override the update route.

To remove Forest Admin’s default behaviour from the route, simply remove the next() statement :wink:

Let me know if this helps!

Hi @adriguy ,

Yes, i checked the documentation and according to that update the information and pass to the API. But here I need to upload images/videos any idea here if we can send the object of data. Or if we can upload those from forest admin to s3 directly and store the image URL of s3 bucket.

getting below request body parameter into the API end. please clear what should be the file size we can upload.

As you suggested to remove the next(). after that facing issue on click save button is keep loading.

Thanks.

@sourabht about the save button keeping loading, I guess you need to trigger a response.send() with the final data.

Did you see this woodshop about uploading files in AWS S3?
This could help you out with your usecase.

Let me know!
Thanks.

1 Like

Thanks @adriguy .

I will try with provided documentation.

1 Like

Hi @adriguy ,

I achieved the functionality update record via API. I get the base64 image from the forest admin request and upload it to s3 then collect the URL and pass it to API request. inside adminsettings route only added the code for upload file to AWS as well.

// Update a Adminsetting
router.put('/adminsettings/:recordId', permissionMiddlewareCreator.update(), async (request, response, next) => {
  // Learn what this route does here: https://docs.forestadmin.com/documentation/v/v6/reference-guide/routes/default-routes#update-a-record
  const recordUpdater = new RecordUpdater('adminsettings');
  const data = request.body.data.attributes;
  const home_header_title = data.home_header_title;
  const home_header_image = data.home_header_image_url;
  
  let home_header_image_url;
  if(home_header_image != undefined){
    home_header_image_url = await uploadFileToAWS(home_header_image, 'home_header_image.png');
  }
  
  const requestData = { 
          home_header_title:home_header_title,
          home_header_image_url : home_header_image_url
  };
  await axios({
      url: process.env.API_URL+'/settings',
      method: 'post',
      data: requestData,
    })
    .then(async (result) => {
      response.send(await recordUpdater.serialize(result.data.data));
    })
    .catch((error) => {
      response.send({'error:': error});
    });
});

const uploadFileToAWS = async (file, filename) => {
              write your code here to upload to s3 Bucket.............
}

Thanks,
Sourabh

1 Like