Context.request call in update function

Hi,
My understanding (from the forum posts here) is that I should call context.request inside plugins JS code (not standard fetch). However, I am getting the error TypeError: context.request is not a function. I’ve tried to find documentation on it or open source plugins that are using external APIs, but was not able to find any.
I suppose the other option would be to use Plugin API calls but haven’t managed to find any examples of how to use it in plugin elements.
please, help, thanks

You sound like you know what you’re doing, so why aren’t you sharing your code if you… you know… want help with your code?

Nobody can guess how it is that you’re generating that error. If you’re passing context to your update function as context then context.request() is def a thing.

1 Like

@keith , I have the following code and am getting the context.request is not a function error. I’m at a loss.

function(properties, context) {
    
    /**
     * For details on the PutObjectCommand, see:
     * https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/client/s3/command/PutObjectCommand
     */
    const { S3Client, PutObjectCommand } = require('@aws-sdk/client-s3');
    // const fetch = require("node-fetch");
    
    // Create the S3Client instance
    const s3Client = new S3Client({
        region: properties.aws_region,
        credentials: {
            accessKeyId: properties.user_access_key,
            secretAccessKey: properties.user_secret_key
        }
    });
    
    // Create the call input
    let input = {
        Bucket: properties.bucket,
        Key: properties.object_key,
    };
    
    // Add file information, if provided
    if (properties.file) {
        
        // Get the file from Bubble's S3 bucket
        const options = {
            uri: "https:" + properties.file,
            method: "GET",
            headers: {
                "content-type": "application/octet-stream"
            }
        };
        const fileStream = context.request(options).body;
        
        input['Body'] = fileStream;
        input['ContentType'] = properties.file.type;
    }
    
    var response = '';
    try {
        // Create the command
        const command = new PutObjectCommand(input);

        response = context.async(async (callback) => {
            try {
                const result = await s3Client.send(command);
                callback(null, result);
            } catch (error) {
                callback(error);
            }
          });
    } catch (error) {
        return ({
            error: `Something went wrong: ${error.message}`
        });
    }

    // Return the success response
    return ({
        expiration: response.Expiration,
        version_id: response.VersionId
    });
}

I feel like I’m missing something silly.

For anyone else that comes across this issue, my problem was that the plugin docs reference the old way of using context.request and context.async. Both of those have been updated since the v4 update.

See here for more info: https://manual.bubble.io/account-and-marketplace/building-plugins/updating-to-plugin-api-v4