How Do I Use JS Functions From Pasted Javascript

Eliot Cole 31 Reputation points
2024-10-21T13:15:13.3333333+00:00

Hi, I'm green to everything here, but I'm having trouble using something that (in my dumb head, at least) should work.

I do not wish to use 'import' as I do not wish my code to import from a link every time in case I am requesting a lot.

Whatever I try, after pasting the full or minified javascript into my Function ... I cannot reference the functions within that.

How do I use the code present in a block of Javascript in my Function App's code?

This is my code, where EliotDocument() or .doThing() are part of the code in 'PASTED_CODE_GOES_HERE' ... and as you can see, I'm modifying the boilerplate HTTP request:

PASTED_CODE_GOES_HERE

module.exports = async function (context, req) {
    context.log('JavaScript HTTP trigger function processed a request.');

    //const name = (req.query.name || (req.body && req.body.name));
    const fileBase64 = req.body.fileContentBase64;
    let ellyDoc = await EliotDocument(fileBase64);
	let ellyDocDoThing = ellyDoc.doThing();
    const yeahYeah = 'Some nonsense';

    /*
    const responseMessage = name
        ? "Hello, " + name + ". This HTTP triggered function executed successfully."
        : "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.";
    */

    context.res = {
        // status: 200, /* Defaults to 200 */
        "headers": {
            "Content-Type": "application/json"
        },
        "body": {
            "ellyData": yeahYeah
        }
    };
};

I have tried:

  1. Pasting (PASTED_CODE_GOES_HERE) as minified code above or below my module.exports bit
  2. Tried the same with the full code
  3. Tried with EliotDocument() and not .doThing()
  4. Similarly tried with only .doThing() ...

... and it just 500s out with every request, either with a 'asdasda' on the EliotDocument, or a 'asdasda' on the .doThing() ... what do I do to use my code?


Yes, you can assume that the code I am pasting is actually legally re-usable and functioning javascript.

If you wish to pick an example from literally anywhere with which to assist me on this, please do so, and replace the EliotDocument() and .doThing() actions with representative ones from there. They are literally just made up examples.

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,039 questions
0 comments No comments
{count} votes

Accepted answer
  1. Khadeer Ali 245 Reputation points Microsoft Vendor
    2024-10-22T01:47:22.86+00:00

    Hi Eliot Cole,

    Welcome to the Microsoft Q&A Platform! Thank you for asking your question here.

    The issue with your current approach is that pasted JavaScript code doesn't automatically become part of your Function App's codebase.

    We could achieve this by including the pasted code as a separate file.

    1. Create a new JavaScript file (e.g., eliot-utils.js) in your Function App's directory.
    2. Paste the entire code from PASTED_CODE_GOES_HERE into the eliot-utils.js file.
    3. In your main Function App code (index.js or the file with module.exports), import the functions using the require statement:

    const EliotDocument = require('./eliot-utils').EliotDocument;

    module.exports = async function (context, req) {

    // ... your existing code

    let ellyDoc = await EliotDocument(fileBase64);

    let ellyDocDoThing = ellyDoc.doThing();

    // ...

    };

    If you find this answer helpful, please click "Accept Answer" and kindly upvote it.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.