尝试从我的数据库MySQL插入HTML [重复]

jv4diomz  于 4个月前  发布在  Mysql
关注(0)|答案(1)|浏览(43)

此问题在此处已有答案

Getting Unexpected Token Export(19个回答)
24天前关闭。
我试图插入HTML从我的数据库MySQL与nodejs。
我从我的数据库中获取产品

export async function getAllProducts() {
    try {
        const response = await fetch('http://localhost:4000/productos');
        
        if (!response.ok) {
            throw new Error(`Error: ${response.status} - ${response.statusText}`);
        }

        const json = await response.json();
        return json;
    } catch (error) {
        console.error('Error fetching products:', error.message);
        throw error;
    }
}

字符串
我试着插入它,但我有这个错误:


的数据

import { getAllProducts } from "../products.js"

//instancias
const contenedor_hamburguesa = document.getElementById("contenedor-hamburguesa");
const contenedor_smash = document.getElementById("contenedor-smash");
const contenedor_ensalada = document.getElementById("contenedor-ensalada");
const contenedor_bebida = document.getElementById("contenedor-bebida");

const fillProducts = async () => {
    const products = await getAllProducts();

    products.forEach(product => {
        const categoria = product.categoria;

        let container;

        // Comparar con strings directamente
        if (categoria === "hamburguesa") {
            container = contenedor_hamburguesa;
        } else if (categoria === "smash") {
            container = contenedor_smash;
        } else if (categoria === "ensalada") {
            container = contenedor_ensalada;
        } else if (categoria === "bebida") {
            container = contenedor_bebida;
        }

        // Crear elemento en la categoría 
        if (container) {
            container.innerHTML += `<div>${product.nombre}</div>`;
        }
    });
}


请帮帮我:)
我试图从我的数据库中获取产品,并使用container.innerHTML发送HTML。

ukdjmx9f

ukdjmx9f1#

请尝尝这个。

async function getAllProducts() {
    // Your code here
}
module.exports = getAllProducts;

const getAllProducts = require("./products.js");

字符串

相关问题