2

I created a static resource with just one JS file. However when component rendered it gives me an error "undefined".

Template:

<!-- videoPlayer.html -->
<template>
    <div class="fancy-border">
        <video autoplay>           
        </video>
    </div>   

</template>

Controller:

/* eslint-disable no-console */
import { LightningElement } from 'lwc';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import { loadScript, loadStyle } from 'lightning/platformResourceLoader';
import instascan from '@salesforce/resourceUrl/instascan';

export default class VideoPlayer extends LightningElement {
    instaInitialized = false;

    renderedCallback() {
        if(!this.instaInitialized){
            // eslint-disable-next-line no-console
            console.log(instascan);
            loadScript(this,instascan)
                .then( () => {
                    this.instaInitialized = true;
                })
                .catch( error => console.log(error));

        }

    }

    initializeD3() {
        const player = this.template.querySelector('video');

    }

}

Static Resource: enter image description here

Error:

enter image description here

4
  • Share your template markup . Commented Aug 26, 2019 at 16:44
  • Can you try the exact LWC recipe? github.com/trailheadapps/lwc-recipes/tree/master/force-app/main/… Commented Aug 26, 2019 at 18:59
  • it is a simple template which has video tag. Added
    – Ajay
    Commented Aug 26, 2019 at 19:31
  • 1
    I would recommend trying loading this script in aura first. Cos, unfortunately, LWC doesn't show error messages when loading scripts. At least I couldn't make them work :(
    – ytiq
    Commented Aug 26, 2019 at 20:07

2 Answers 2

0

Error is undefined when the script is not found.

The only way I got this to work is to put the javascript file into a zip file. Next on the loadScript function you pass the path to the file like this (change the string if you are using subfolders).

loadScript(this, instascan + '/instascan.js'
-1

loadScript function needs the path to your js as the second argument. Your code above does not specify that.

See documentation https://developer.salesforce.com/docs/component-library/bundle/lightning-platform-resource-loader/documentation

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .