Error encountered in JavaScript/ThreeJS: Unable to access property 'set' as it is undefined

I am currently working on creating a unique custom object using ThreeJS called Model. This object is made up of other custom objects that I have defined, such as Part. The problem arises when I encounter the following error:

const Model = function() {
      this.mesh = new THREE.Object3D();
      this.mesh.name = 'model';

      //creating an instance of the Part object
      this.lowerPart = new Part();
      this.lowerPart.position.set(1, 2, 3, -75);  //Error occurs here
      this.lowerPart.rotation.set(0, 0, 0);
      this.mesh.add(this.lowerPart);
}

Upon running the program, I receive an error message stating that it cannot read the property 'set' of undefined, specifically referencing

this.lowerPart.position.set(1, 2, 3, -75);
.

This is how I have defined the Part object:

 const Part = function () {
      this.mesh = new THREE.Object3D();
      this.mesh.name = 'part';

      const partShape = new THREE.Shape();
      partShape.lineTo( 40, 80 );
      partShape.lineTo( 60, 80 );
      partShape.lineTo( 60, 100 );
      partShape.lineTo( 40, 100 );

      //extrude settings defined....

      const partGeometry = new THREE.ExtrudeGeometry(partShape, extrudeSettings);
      const partMesh = new THREE.Mesh(partGeometry, new THREE.MeshStandardMaterial({
        color: 0x1c4bc9,
        flatShading: true
      }));
      this.mesh.add(partMesh);
    };
    Part.prototype = Object.create(THREE.Object3D.prototype);
    Part.prototype.constructor = Part;

In my Model function, there is also another issue where this.lowerPart is labeled as unused. Despite being of type Object3D with the necessary properties, I am unsure of the reason for this indication.

The console confirms that this.lowerPart is indeed an instance of Part.

I have explored various suggestions from StackOverflow related to similar issues, but none have resolved my problem. One of the most relevant discussions can be found here: Uncaught TypeError: Cannot read property 'set' of undefined Unfortunately, these solutions did not work in my case.

If anyone has any suggestions or insights on how to address and resolve this issue, I would greatly appreciate your input!

Answer №1

In order to inherit characteristics from THREE.Object in Part, it is essential to include the following line of code within the constructor of Part (ensure it is the first line in the ctor):

THREE.Object3D.call( this );

By doing so, you will be able to utilize attributes such as position, rotation, or scale with an instance of Part.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Access information from Google Sheets through an AJAX GET request

I am facing an issue with my code while trying to retrieve data from a Google spreadsheet that I have already published. I have set the share properties of the spreadsheet to 'anyone can edit' and provided the correct URL, but I am still encounte ...

React JS - Breaking down the distinction between PublicTheme and PublicTheme

In my React project, I am currently working on creating the admin dashboard and designing the UI area for user interaction. I have encountered an issue where I am unable to separate the admin theme from the PublicTheme. Even when navigating to "/admin/lo ...

What is the best way to insert a new item into an array that is nested within an object?

Currently, I am delving into the world of using $resource in angularjs and finding great examples in this answer AngularJS $resource RESTful example. Fetching and creating records is working fine, but now my challenge lies in adding a "section" to an exist ...

Retrieve the previous or parent function using jQuery

How to Customize lightSlider Next/Prev Buttons in AngularJS Directive app.directive('lightSlider',function(){ return{ link:function(scope,element,attrs){ scope.slider = $(element).lightSlider({ item: 4, ...

Managing post requests in node.js using busboy and then multer

I'm currently facing an issue with busboy (or multiparty) and multer while trying to parse my request. Initially, the request is received successfully using busboy, where I proceed to create a folder and update my database. However, when I attempt to ...

Difficulty triggering an event within a collection in Backbone.js

Having recently delved into JavaScript and Backbone, I encountered a puzzling error. Router = Backbone.Router.extend({ routes: { ":albumID": "load" }, load: function (albumID) { if (controller.collectionInitialized == true) ...

Tips for including absent items in an array

Looking to fill in missing weeks and years in an array of objects that have a reportYear and reportWeek property. How can I insert additional objects to cover all weeks from the beginning date to the end date? For example: Input: [ {"reportYear":2017, ...

Issue with subscribing in a MEAN stack application

Currently, I have completed the backend development of my application and am now working on the frontend. My focus at the moment is on implementing the register component within my application. Below is the code snippet for my Register Component where I a ...

Switching the cursor to the following input after a paste event using JQuery

I am currently working on an HTML form that consists of multiple input boxes. I'm looking to focus on the next input box after pasting content into one of them. Below is the code snippet I have been using: $("input").bind('paste', function( ...

Illustration of a D3 graph demo

I'm currently working on modifying a d3.js graph example originally created by Mike Bostock. My goal is to replace the d3.csv method with d3.json. I made adjustments to parts of the original code, but unfortunately, my graph has disappeared without an ...

Is there a way I can ensure the values are loaded when the page loads, rather than displaying NaN?

I've recently created a car rental calculator for a client, and it's almost complete. Everything is working smoothly, from the calculations to the conditions. However, I'm facing an issue where the price isn't calculated on page load. I ...

What is the best way to search for a CSS selector that includes an attribute beginning with the symbol '@'?

Whenever I want to target an element with a click event, I usually use the following jQuery selector: $('div[@click="login()"]') <div @click="login()"> Login </div> However, when I tried this approach, it resulted ...

Store Dark Mode preferences in the browser's local storage using React and Material-UI

Is there a way to save the dark mode setting in localStorage? Can this approach be used for storing it? Any suggestions on how to achieve this would be appreciated. Thanks, cheers! function App() { const [darkState, setDarkState] = useState("&qu ...

Send text values to a JavaScript function that includes unique characters

Hi there, can you help me figure out how to pass the following value as an argument in a JavaScript function? I am receiving this value dynamically from a Java string variable like so: String vals= "The apostrophe ( ’ or ' ) is a punctuation < ...

mongoDB utilizes the timestamp in the _id to determine if it has been accessed in the past 24 hours

After months of lurking and finding answers, I am finally posting for the first time. I am excited to contribute back to the community, although I am still new to programming having just started a few months ago. Here's my dilemma: I am running a web ...

Is it possible to automatically adjust the cursor position in a textarea when the language is switched?

I am working with a textarea that needs to support both English and Arabic languages. In English, text is typically left-aligned, so the cursor should start on the left-hand side. However, in Arabic, text is right-aligned, meaning the cursor should begin ...

Commit the incorrect file name with the first letter capitalized

There seems to be an issue with the git not recognizing the correct filename casing. I have a file named User.js in my workspace, but when checking the git status, it displays user.js instead. Despite repeatedly changing and committing as User.js, the gi ...

Could you explain the process of utilizing $.each to showcase various nested elements within the same JSON object?

I am currently working on utilizing the Trello API in order to develop a user stories and checklist items dashboard for a client. Below is the code snippet that pertains to this project: Trello.get("boards/z6yBuVol/cards", function(cards) { $cards ...

The xhr.upload event listener is triggered when the xhr.responseText is empty after loading

const request = new XMLHttpRequest(); request.open('put', url, false); request.upload.addEventListener('load', function(e) { alert(request.responseText); }, false); Why is the responseText property of the XMLHttpRequest object empt ...

Exiting or returning from a $scope function in AngularJS: a guide

There are times when I need to exit from my $scope function based on a certain condition. I have attempted to achieve this using the return statement. However, my efforts have been in vain as it only exits from the current loop and not from the main scop ...