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.