Uncaught ReferenceError: function is not defined at HTMLSelectElement.onchange

I’m displaying the movie data that I’ve brought in open api in card format. I’d like to newly arrange them in the order of title a-z and highest stars in drop-down format. However, it does not work when I select another item after the initial card is loaded I thought it was a problem with the function, so I used console.log, but the function works fine.

Uncaught ReferenceError: handleSortChange is not defined
The error at HTMLSelectElement.onchangeappears And alsosort.js:50 TypeError: Cannot read properties of undefined (reading ‘slice’)
at sortByTitle (sort.js:4:17)
at handleSortChange

I put <script src="./src/sort.js" type="module"></script> in the header of index.html because it said it had to be loaded before the script was executed

sort.js

import { callGetMoviesAPI, displayMovieData } from "./movieAPI.js";

const sortByTitle = (movies) => {
  return movies.slice().sort((a, b) => {
    const titleA = a.title.toUpperCase();
    const titleB = b.title.toUpperCase();
    if (titleA < titleB) {
      return -1;
    }
    if (titleA > titleB) {
      return 1;
    }
    return 0;
  });
};

const sortByRating = (movies) => {
  return movies.slice().sort((a, b) => b.vote_average - a.vote_average);
};
const movies = [
  { title: "B", vote_average: 8.5 },
  { title: "A", vote_average: 7.9 },
  { title: "C", vote_average: 9.2 }
];

console.log("Sorted by title:", sortByTitle(movies));
console.log("Sorted by rating:", sortByRating(movies));

const handleSortChange = async (value) => {
  try {
    const movies = await callGetMoviesAPI();
    console.log("Movies from callGetMoviesAPI:", movies); // Movies from callGetMoviesAPI: undefined

    switch (value) {
      case "default":
        displayMovieData(movies);
        break;
      case "title":
        const sortedMoviesByTitle = sortByTitle(movies);
        displayMovieData(sortedMoviesByTitle);
        break;
      case "rating":
        const sortedMoviesByRating = sortByRating(movies);
        displayMovieData(sortedMoviesByRating);
        break;
      default:
        displayMovieData(movies);
    }
  } catch (err) {
    console.error(err);
  }
};

export { handleSortChange };

main.js

import { handleSortChange } from "./sort.js";
import { callGetMoviesAPI, displayMovieData } from "./movieAPI.js";
import { search_movie } from "./search.js";

const urlParams = new URLSearchParams(window.location.search);
const searchMovies = urlParams.get("search");

const movies = JSON.parse(localStorage.getItem("movies"));

if (JSON.parse(localStorage.getItem("movies")) === null) {
  console.log("----load move datas----");
  callGetMoviesAPI();
} else if (searchMovies === null) {
  const filteredMovieData = movies.map((movie) => JSON.parse(localStorage.getItem(movie)));
  displayMovieData(filteredMovieData);
} else {

  let contentMovie = search_movie(searchMovies);
  displayMovieData(contentMovie);
}


document.getElementById("sort").addEventListener("change", (event) => {
  handleSortChange(event.target.value);
});

index.html

<body>
    <movie-header></movie-header>

    <div class="dropdown">
      <label for="sort">arrange</label>:</label>
      <select id="sort" onchange="handleSortChange(this.value)">
        <option value="default">defalut</option>
        <option value="title">title</option>
        <option value="rating">rating</option>
      </select>
    </div>

I’m sorry if my question was rude. I’m a novice at developing and this is my first question

I checked with console.log to see where and what was going on, and I looked up similar questions in stack overflow, but I’m not sure. When I select drop-down, I want to see a list of newly sorted cards

New contributor

조은영 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật