cant populate shadcn ComboBox component with data. (Next.JS, Postgres SQL)

Stack: Next.JS, Postgres SQL (hosted from vercel), tailwindcss, shadcn

I am having issues populating data for the Shadcn combobox component. The page renders and it defaults to “Select Case”:

Here are some console logs after page load (without clicking anything):

Cases state updated: []
Value state updated: null
Cases state updated: []
Value state updated: null
[Fast Refresh] rebuilding
Fetched cases: (2) [{…}, {…}]
Cases state updated: (2) [{…}, {…}]
[Fast Refresh] rebuilding
Fetched cases: (2) [{…}, {…}]
Cases state updated: (2) [{…}, {…}]

The data is fetched properly but then when I click the component to get a dropdown with the values listed… I get this

RuntimeError:TypeError: undefined is not iterable (cannot read property Symbol(Symbol.iterator))
at Function.from (<anonymous>)
at A (index.mjs:1:3990)
at U (index.mjs:1:3003)
at index.mjs:1:1763
at index.mjs:1:10381
at Map.forEach (<anonymous>)
at index.mjs:1:10370
at commitHookEffectListMount (react-dom.development.js:21102:23)
at commitHookLayoutEffects (react-dom.development.js:21212:7)
at commitLayoutEffectOnFiber (react-dom.development.js:21410:11)
at recursivelyTraverseLayoutEffects
'use client';

import * as React from 'react';
import { Check, ChevronsUpDown } from 'lucide-react';

import { cn } from '@/lib/utils';
import { Button } from '@/components/ui/button';
import {
  Command,
  CommandEmpty,
  CommandGroup,
  CommandInput,
  CommandItem,
} from '@/components/ui/command';
import {
  Popover,
  PopoverContent,
  PopoverTrigger,
} from '@/components/ui/popover';

interface Case {
  id: number;
  name: string;
}

export function ClientCombobox() {
  const [open, setOpen] = React.useState(false);
  const [value, setValue] = React.useState<string | null>(null);
  const [cases, setCases] = React.useState<Case[]>([]);

  React.useEffect(() => {
    async function fetchCases() {
      try {
        const response = await fetch('/api/cases');
        if (response.ok) {
          const data = await response.json();
          console.log('Fetched cases:', data);
          setCases(Array.isArray(data) ? data : []); // Ensure data is an array
        } else {
          console.error('Failed to fetch cases');
        }
      } catch (error) {
        console.error('Error fetching cases:', error);
      }
    }

    fetchCases();
  }, []);

  React.useEffect(() => {
    console.log('Cases state updated:', cases);
    // If the value is no longer in the cases array, reset it
    if (value && !cases.some(caseItem => caseItem.id.toString() === value)) {
      setValue(null);
    }
  }, [cases]);

  React.useEffect(() => {
    console.log('Value state updated:', value);
  }, [value]);

  const handleSelect = (currentValue: string) => {
    console.log('Selected value:', currentValue);
    setValue(currentValue === value ? null : currentValue);
    setOpen(false);
  };

  return (
    <Popover open={open} onOpenChange={setOpen}>
      <PopoverTrigger asChild>
        <Button
          variant="outline"
          role="combobox"
          aria-expanded={open}
          className="w-[200px] justify-between"
        >
          {value ? cases.find((caseItem) => caseItem.id.toString() === value)?.name : 'Select case...'}
          <ChevronsUpDown className="ml-2 h-4 w-4 shrink-0 opacity-50" />
        </Button>
      </PopoverTrigger>
      <PopoverContent className="w-[200px] p-0">
        <Command>
          <CommandInput placeholder="Search case..." />
          <CommandEmpty>No case found.</CommandEmpty>
          <CommandGroup>
            {cases.length > 0 ? (
              cases.map((caseItem) => (
                <CommandItem
                  key={caseItem.id}
                  value={caseItem.id.toString()}
                  onSelect={handleSelect}
                >
                  <Check
                    className={cn(
                      'mr-2 h-4 w-4',
                      value === caseItem.id.toString() ? 'opacity-100' : 'opacity-0'
                    )}
                  />
                  {caseItem.name}
                </CommandItem>
              ))
            ) : (
              <CommandEmpty>No case found.</CommandEmpty>
            )}
          </CommandGroup>
        </Command>
      </PopoverContent>
    </Popover>
  );
}

I would like the combobox dropdown to display the names from the fetched data.

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