2

When viewing files in Autodesk forge viewer, getting the following errors,

  1. Cannot read property ‘setEndpoint’ of undefined. Screenshot

  2. doc.getRootItem is not a function. Screenshot

And will be able to view after empty cache and hard reload (Ctrl + Shift +R) the page. Sometimes the same errors will persist even after hard reload and clearing browser cache.

Code for the second error.

var options = {
    env: 'AutodeskProduction',
    accessToken: getAccessToken() //Method to get access token- no errors here
};
var documentId = 'urn:' + urn;
Autodesk.Viewing.Initializer(options, function onInitialized() {
    Autodesk.Viewing.Document.load(documentId, onDocumentLoadSuccess, onDocumentLoadFailure);
});

//Autodesk.Viewing.Document.load - success function.
function onDocumentLoadSuccess(doc) {
    setTimeout(function() {
        debugger;
    }, 5000);

    //Error is thrown in the line below.                
    var viewables = Autodesk.Viewing.Document.getSubItemsWithProperties(doc.getRootItem(), {
        'type': 'geometry'
    }, true); //throws error on calling doc.getRootItem()

    if (viewables.length === 0) {
        console.error('Document contains no viewables.');
        return;
    }
    // Choose any of the avialble viewables
    var initialViewable = viewables[0];
    var svfUrl = doc.getViewablePath(initialViewable);
    var modelOptions = {
        sharedPropertyDbPath: doc.getPropertyDbPath()
    };

    var viewerDiv = document.getElementById('divViewer');
    viewer = new Autodesk.Viewing.Private.GuiViewer3D(viewerDiv);
    viewer.start(svfUrl, modelOptions, onLoadModelSuccess, onLoadModelError);
}
georgyfelix
  • 368
  • 2
  • 13

1 Answers1

4

You must have unwitting upgraded to Viewer V7 with which getRootItem and several other functions have been deprecated - see here for its release notes and migration guide.

Stick with V6 with <script src="https://developer-stg.api.autodesk.com/modelderivative/v2/viewers/viewer3D.js?v=6.6"></script> - if you don't specify a version by default the latest stable version would be served which is now V7.0.

Bryan Huang
  • 5,247
  • 2
  • 15
  • 20