Issue Encountered: THREE.js - TypeError Uncaught

I recently delved into the world of Web GL and THREE.js

After following a tutorial on YouTube, I attempted to display a cube and an axis using the code below. Unfortunately, upon trying to view the page with this code, I encountered a Javascript error message:

Uncaught TypeError: this.updateMorphTargets is not a function

Seeking guidance from those well-versed in THREE.js, I'm unsure where I went wrong. Any assistance would be greatly appreciated. Thank you for your time.

jQuery(document).ready( function($){

    var scene = new THREE.Scene();
    var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight,.1, 500);
    var renderer = new THREE.WebGLRenderer();

    renderer.setClearColor(0x000000);
    renderer.setSize(window.innerWidth, window.innerHeight);

    var axis = new THREE.AxisHelper(10);

    scene.add(axis);

    var cubeGeometry = new THREE.BoxGeometry(5, 5, 5);
    var cubeMaterial = new THREE.MeshBasicMaterial({color: 0xdddddd, wireframe:true});
    var cube = THREE.Mesh(cubeGeometry, cubeMaterial);

    cube.position.x = 0;
    cube.position.y = 0;
    cube.position.z = 0;

    scene.add(cube);

    camera.position.x = 40;
    camera.position.y = 40;
    camera.position.z = 40;

    camera.lookAt(scene.position);

    $('#webgl-container').append(renderer.domElement);
    renderer.render(scene, camera);

});

Answer №1

In your code, there was a minor mistake where you forgot to use the new operator before Three.MESH. The corrected version should look like this:

var cube = new THREE.Mesh(cubeGeometry, cubeMaterial);

The importance of the new operator cannot be overstated. Without it, the THREE.Mesh function becomes just a regular function rather than a constructor. This results in the this keyword inside the function referring to the THREE namespace object instead of a newly created Mesh object. Since the THREE namespace object does not have an updateMorphTarget() method, you encountered an error.

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

How come my fixed navigation bar is appearing above my sticky navigation bar even though I positioned it at the bottom in the code?

My current project involves creating a sticky bar and fixed navbar using Bootstrap 5. I structured my code with one file for the sticky navbar and another file for the fixed navbar. The challenge I faced was having the fixed navbar overlap the sticky navba ...

Pass the response from a JavaScript confirm dialog to a Ruby program

Apologies if this question seems naive, but I am curious if it is feasible to pass the response from a JavaScript confirm box or message box as a value to a Ruby program. If I were to click on the "yes" button in the confirm box, could this response be st ...

What is the most effective way to manage and respond to multiple events while also organizing the overall structure of

I am currently in the process of planning a complex web application using WebGL and Three.js. As I experiment with different tests, I have encountered a problem that is raising many questions for me. I am unsure of the correct approach to take and would gr ...

Spinning 3D Grid in Global Coordinates - Using THREE.js

I'm struggling with rotating an object in my project. I am currently utilizing THREE.Shape to define a custom shape. After creating the shape, I convert it into a Mesh and set its position to: buildingMesh.position.set(-70, -300, levelY); However, d ...

Updating front end data using Node.js - A Comprehensive Guide

I am looking to create a website using node.js that can dynamically display a plot and update the data every minute without needing to refresh the entire page. As someone new to node.js, I am interested in learning how to use "get" requests to update the d ...

Using V-model to bind to an object dynamically created in an array

I am trying to create objects dynamically in an array and use v-model with an input. Here is my code snippet: This is the array structure new_questions:{ questionrecord: [] } This is the input field setup <div class="form-fields" v-for="(field, ...

Avoiding the activation of router middleware

According to Express documentation: A route will match any path that follows its path immediately with a “/”. For example: app.use('/apple', ...) will match “/apple”, “/apple/images”, “/apple/images/news”, and so on. If I ...

Using PHP to auto populate HTML form

In my application, I have two forms. The first form takes user input with a post action and runs a SELECT query with a WHERE clause using PHP in the backend. The retrieved value is stored in a session variable to make it global. I then display this session ...

Certain images are being rotated in a counter clockwise direction by 90 degrees

Users can upload images of their items, but some pictures appear rotated 90 degrees counter-clockwise after uploading. This could be due to the way the photos were taken on an iPhone. Is there a simple solution to correct this rotation issue? The following ...

"Unfortunately, the JavaScript external function failed to function properly, but miraculously the inline function

Struggling to create a fullscreen image modal on the web? I was able to get it working fine when embedding the script directly into my HTML file, but as soon as I moved it to an external JS file, things fell apart. I've double-checked all the variable ...

What is the process for separating static methods into their own file and properly exporting them using ES6?

After exploring how to split up class files when instance and static methods become too large, a question was raised on Stack Overflow. The focus shifted to finding solutions for static factory functions as well. The original inquiry provided a workaround ...

Three.js - creating stunning 3D visuals that seamlessly blend with 2D images

I am currently working on a project in three.js that involves creating a movable 3D-view on top of a static 2D-rectangle image. While everything is functioning properly, I am facing an issue where the buildings at the upper edge are not visually aligned co ...

Troubleshooting: Javascript success callback not executing upon form submission

A snippet of my JavaScript looks like this: $(document).ready(function(){ $("#message").hide(); $("#please_wait_box").hide(); $("#updateinvoice").submit(function(e){ $("#message").hide(); ...

Setting up TailwindCSS in Nuxt3: A step-by-step guide

Looking to customize the default font Proxima Nova from TailwindCSS in my Nuxt3 project but navigating the file structure is a bit tricky for me. I've gone ahead and installed the tailwindcss module: npm i -D @nuxtjs/tailwindcss and added the module ...

regex for extracting a hyperlink tag from an HTML snippet in a server-side environment

On the server-side, I have an HTML page source stored as a string. My goal is to extract <link rel="icons"... elements from the string and store them in an array. There may be multiple <link rel="icons"... elements with the same starting tag, and I ...

Tips for preventing errors in JavaScript date formatting

Currently, I am utilizing the DHTMLX Scheduler within my web application and aiming to access data for each event. Fortunately, I discovered a method to achieve this using scheduler._events which provides the following information: 1581498064943: {…} ​ ...

Timezones not synchronizing properly with Moment.js

Hello everyone, I am currently exploring the world of moment.js along with the timezone add-on. I'm encountering some issues with a world clock project that involves using moment.js and moment-timezone.js together on a page where highcharts are also p ...

Having difficulty extracting only names from the database with mongoose

My goal is to retrieve the value of all the name keys stored in my database. Each document in the database has only one key, which is the "name" key. Below is the code snippet that I need assistance with: user.find({}, 'name', function(err, user ...

Make the active tab stand out in the navigation menu

Could you please advise on how to highlight the active tab in a menu when users switch to another page? This is my current code: .nav { float: left; font-size: 125%; } .nav ul { margin: 0; } .nav li { background: none repeat scroll 0 0 #77 ...

Troubleshooting: Implementing JavaScript to dynamically append elements to a list on a webpage

Can someone help me troubleshoot why my Chrome extension isn't working properly? I'm trying to create a simple To-do list extension for myself using the code below: HTML: <html> <head> <link rel="stylesheet" typ ...