1

I am just getting started with TypeScript (and front end development in general) coming from a c# background so sorry if this is a really basic question, but I can't figure out where I'm going wrong...

What I'm trying to do for now is create a really basic program to retrieve some sample data from a url in JSON format, parse to TS classes, and display it on the page.

In order to get the json response I found this answer that recommends using a node package. I got it installed and it seems to be ok (at least TS doesn't give me any errors).

I also figured out that I need to compile (not sure if that's the right term?) with Browserify to make it browser compatible since it's using a node module. I did that but now when I try to run in a browser it's telling me my method is not defined.

export class Keynote {
KeyValue: string;
Description: string;
Parent: string;
}

Retrieval class is:

import {Keynote} from "./Keynote";
import * as request from "request-promise-native";

function GetKeynotes(): Array<Keynote> {
    const baseUrl = 'https://revolutiondesign.biz/Sandbox/TypeScript/KeynoteProvider.php';
    var options = {uri: baseUrl};

    const result = JSON.parse(request.get(options));
    return result;
}

and html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Keynotes Testing</title>
    <script src="bundle.js"></script>
    <script>
        function Retrieve() {
            var notes = GetKeynotes();
            document.getElementById('container').innerText = JSON.stringify(notes);
        }
    </script>
</head>
<body>
<div>
    <button content="Get some notes" onclick="Retrieve()">Get some notes</button>
</div>
<div id="container">

</div>
</body>
</html>

Browserify is really long so I didn't want to copy here but you can see it at https://revolutiondesign.biz/Sandbox/TypeScript/KeynoteDisplay.html in the source if you want.

When I click the button I get this error in the browser:

KeynoteDisplay.html:9 Uncaught ReferenceError: GetKeynotes is not defined
    at Retrieve (KeynoteDisplay.html:9)
    at HTMLButtonElement.onclick (KeynoteDisplay.html:16)
Retrieve @ KeynoteDisplay.html:9
onclick @ KeynoteDisplay.html:16

GetKeynotes is defined in my typescript, and on the 5th line of bundle.js I see a function with that name... Why is it undefined?

UPDATE

Ok I have played with jspm and SystemJs but I still don't have something right. I referenced the module with jspm and did a bundle to build.js and uploaded the whole thing just to make sure everything is there. Here are the tags in my html for scripts:

<script src="../../jspm_packages/system.js"></script>
<script src="../../config.js"></script>
<script src="build.js"></script>
<script>
    System.import("Sandbox/TypeScript/build.js")
    function Retrieve() {
        System.import("Sandbox/TypeScript/build.js")
        var notes = GetKeynotes();
        document.getElementById('container').innerText = JSON.stringify(notes);
    }
</script>

When I press the button I can debug in my function, but it still gives the error, 'GetKeynotes is not defined' just like before... Again I can see a function with that name in the build.js file so I don't understand why it's not finding it.

I also tried System.import("Sandbox/TypeScript/KeynoteRetrieval.js") but it gives the error:

Uncaught (in promise) Error: (SystemJS) Node tls module not supported in browsers.
    Error loading https://revolutiondesign.biz/Sandbox/TypeScript/KeynoteRetrieval.js
2
  • 1
    if you are going to use modules in your code you will need to use a module loader to load them as well. Look up SystemJs. Commented Dec 18, 2018 at 16:00
  • Ok I thought Browserify was a module loader, no? Do I need both SystemJs and Browserify? Also if there is a better way to get the url result without modules I'm open to that option for now too... Commented Dec 18, 2018 at 16:05

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.