The concept of an undefined property

I encountered this error in my console:

index.js:70 Uncaught TypeError: Cannot read property 'searchForm' of undefined
    at eval (index.js:70)
    at Module../src/js/index.js (bundle.js:4245)
    at __webpack_require__ (bundle.js:20)
    at eval (webpack:///multi_(:8080/webpack)-dev-server/client?:3:18)
    at Object.0 (bundle.js:4292)
    at __webpack_require__ (bundle.js:20)
    at bundle.js:84
    at bundle.js:87
(anonymous) @ index.js:70
./src/js/index.js @ bundle.js:4245
__webpack_require__ @ bundle.js:20
(anonymous) @ client:3
0 @ bundle.js:4292
__webpack_require__ @ bundle.js:20
(anonymous) @ bundle.js:84
(anonymous) @ bundle.js:87
mixpanel-2-latest.min.js:88 document not ready yet, trying again in 500 milliseconds...
client:52 [WDS] Live Reloading enabled.
client:126 [WDS] Warnings while compiling.
warnings @ client:126
(anonymous) @ socket.js:47
sock.onmessage @ SockJSClient.js:58
EventTarget.dispatchEvent @ sockjs.js:170
(anonymous) @ sockjs.js:887
SockJS._transportMessage @ sockjs.js:885
EventEmitter.emit @ sockjs.js:86
WebSocketTransport.ws.onmessage @ sockjs.js:2961
client:135 ./src/js/views/searchView.js 6:9-17
"export 'elements' was not found in './base'
warnings @ client:135
(anonymous) @ socket.js:47
sock.onmessage @ SockJSClient.js:58
EventTarget.dispatchEvent @ sockjs.js:170
(anonymous) @ sockjs.js:887
SockJS._transportMessage @ sockjs.js:885
EventEmitter.emit @ sockjs.js:86
WebSocketTransport.ws.onmessage @ sockjs.js:2961
client:135 ./src/js/index.js 66:0-8
"export 'elements' was not found in './views/base'

Upon investigating the issue during the ES6 to ES5 conversion by Babel, I pinpointed it here:

_views_base__WEBPACK_IMPORTED_MODULE_2__["elements"].searchForm.addEventListener('submit', function (e) {

Index.js:

// Always ensure that you have the correct directory 

// Import field 
import Search from './models/Search'; 
// Import all functions from the view 
import * as searchView from './views/searchView'
import {elements} from './views/base'; 

/* Global state of the app
    - Search object 
    - Current recipe object
    - Shopping list object
    - Liked recipes
*/

// Empty the app everytime it is reloaded 
const state = {}
const controlSearch = async () =>{
    // 1) Get the query from the view 
    const query =  searchView.getInput;
    console.log(query);

    if(query){
        // 2) Create a new search object and add it to state 
        state.search = new Search(query); // new instance of the search class 

        // 3) Prepare UI for results 

        // 4) Search for recipes 
        await state.search.getResults();

        // 5) Render results in the UI, remember to hit the search button 
        console.log(state.search.result);

    }
}
elements.searchForm.addEventListener('submit', e =>{
    e.preventDefault();
    controlSearch();
})

const search =  new Search('pizza');
console.log(search); 
search.getResults();

searchView.js:

// If we are in the current folder then it is simply base 
import {elements} from './base'; 
// Return the input value from the field 
// Implicit search 
export const getInput =() => elements.searchInput.value; 

base.js:

// All DOM elements will be in this class 
export const element = {
    searchForm: document.querySelector('.search'),
    searchInput: document.querySelector('.search__field')
}

While tracking down the error, it seems everything is defined correctly and I cannot figure out how this error occurred. As someone new to web development, I am hoping for assistance from a more experienced individual to analyze the error. Thank you in advance.

Answer №1

Your base.js file exports an element.

export const element

However, in your index.js file, you are importing elements.

import {elements} from './views/base'; 

You may want to consider changing 'elements' to 'element' or vice versa.

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

Tips on saving an array as a variable

Can anyone help me figure out how to store a PHP variable into a JavaScript variable? var myArray; function retrieveData(id) { $.post( "main/updater.php", { id:id } ).done(function( data ) { myArray = data; }); } I&ap ...

React: Unexpected error occurs with invalid element type

I encountered the following issue while attempting to utilize a component Uncaught Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forg ...

Issue with npm and Python causing asset precompilation to fail during Rails deployment

While setting up on a Ubuntu 20.04 server 01 $HOME/.rbenv/bin/rbenv exec bundle exec rake assets:precompile 01 yarn install v1.22.19 01 [1/4] Resolving packages... 01 [2/4] Fetching packages... 01 [3/4] Linking dependencies... ...

Having difficulty grasping the concept behind the prepend method's functionality

I encountered an issue with my code, where I successfully created a <th> element initially, but faced problems when trying to create it repeatedly. function createTH(){ var noOfRow = document.getElementById("addItemTable").rows.length; var t ...

Switching from using fs.readFile to fs.createReadStream in Node.js

I have a dilemma regarding my code, which involves reading an image from a directory and sending it to index.html. My goal is to replace fs.readFile with fs.createReadStream, but I am struggling to figure out how to implement this change as I cannot seem ...

Ways to simultaneously install numerous gulp packages using node-package-manager

Recently, I made the transition to using the gulp task runner for automating my workflow. However, whenever I start a new project, I find myself having to install all the required packages listed in the gulpfile.js by running the following command: npm in ...

Using the window.prompt function to send information to specific fields in a MySQL database is not functioning correctly. I am looking for assistance with this issue

Currently, I am attempting to send some data to a server that is MySQL-based. After running the code below, it seems like there are no errors showing up in the console. Can you please review this code and let me know if you spot any issues? I would really ...

Display content exclusively in PDF format

On my HTML page, I have two sections - one for inputting values and the other for viewing a PDF. To ensure that the PDF view is hidden until explicitly requested, I need it to remain invisible by default. It should only appear as a PDF when someone clicks ...

Can the Twitter Bootstrap typeahead feature be used with an external data source?

Can the typeahead feature in version 2.0.3 of twitter-bootstrap work with a remote data source? You can check out the link for more information on the typeahead functionality: ...

Passing the title of a page as data to a component in Next.js

I am currently working on setting a custom title for each page within my next.js project. Here is an example of a simple Layout: const MainLayout = props => { return ( <Layout style={{ minHeight: "100vh" }}> <Head> < ...

Image encoded in base64 not appearing on the screen

Hey there, I'm currently working on implementing a jQuery image uploader that generates a base64 code when an image is selected. function readImage(input) { if (input.files && input.files[0]) { var FR= new FileReader(); FR.onload ...

Search MongoDB using regular expressions that disregard any punctuation marks

I'm currently working on querying a MongoDB collection to find a single item based on a string property using react-router-dom's NavLink. The challenge arises when the prop via NavLink contains hyphens instead of spaces, and there are also names ...

What is the best way to combine two responses and then convert them into a promise?

When making two calls, the firstCallData prints data fine. However, when I use + to merge the responses, it returns me the following Response. What is a better approach to achieve this task? main.ts let data = await this.processResponse(__data.Detail ...

Images within inline blocks appear to be extending beyond their containing div as though they are

When I check my website in various browsers such as Firefox, IE, Edge, and Safari, I noticed that the images in the "My Specialties" section of the left column are extending outside of the div instead of fitting within it. This problem also occurs in Chrom ...

Incorporate a unique attribute into a select tag using Javascript

My goal is to dynamically generate a select element using JavaScript and include a custom attribute called data-placeholder. Is there a way to add non-standard attributes using JavaScript without triggering a browser error like: Uncaught referenceError: ...

Unleashing the power of RollupJs: A guide to dynamically bundling modules and objects

Is there a way to dynamically bundle a module/object into my RollupJs output file? I have experimented with various options without success in achieving the desired result. Below is a brief sample project that demonstrates what I am trying to achieve. The ...

The alert message fails to appear during an Ajax request

I'm having trouble understanding why the Ajax function in this code snippet is not triggering the success or error functions: function get_cust_key(custid) { $.ajax({ type: "POST", url: 'http://localhost/Test/index.php/getCust ...

Exploring Elasticsearch: Uncovering search results in any scenario

I've been working on a project where my objective is to receive search engine results under all conditions. Even if I enter a keyword that is not included in the search data or if it is an empty string, I still want to get some kind of result. How can ...

Issue encountered when attempting to batch delete documents within a subcollection: Error message displays "TypeError: n.indexOf is not a

When attempting to batch delete a document within a subcollection, I encounter some issues. There are scenarios where I may need to remove the same product document ID along with various history document IDs, or multiple product document IDs with differen ...

Difficulty arising from implementing v-if as a filter within a v-for loop in Vue.js

I'm struggling a bit with setting up a conditional statement using v-if along with a loop using v-for in Vue. Here's what I have so far: <div class="row form-group" v-for="(article, key, index) in articles" :key="key" v-if="article.pubdate(fi ...