Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

183
Vistas
NextJS array mapping for POST request

I am learning Next.js currently, and I have a question.

I have this data as a req.body:

{
    "name": "Name Here",
    "phone": [
        {
            "phonetype": "Work",
            "phonenumber": "PhoneNumber1"
        },
        {
            "phonetype": "Home",
            "phonenumber": "PhoneNumber2"
        }
    ],
    "city": "CityHere"
}

And I am trying to create a new Customer in the database. Below is my POST:

case 'POST':
    try{
        const customer = req.body;
        const newCustomer = new Customer({
            name: customer.name,
            //phone: HOW TO I HANDLE THIS ARRAY?
            city: customer.city,
        });

        const createdCustomer = await Customer.create(newCustomer);
                
        res.status(201).json({success: true, data: createdCustomer});
    } catch (error) {
        res.status(400).json({success: false, data: "Error Creating Customer"});
    }
    break;

How do I go about submitting it to the database (using Mongoose)?

Schema below:

const CustomerSchema = new Schema({
    name: {
        type: String,
        required: true,
        trim: true,
    },
    phone: {
        phonetype: {
            type: String,
        },
        phonenumber: {
            type: String,
        },
    },
    city: {
        type: String,
        required: true,
        trim: true,
    }
})
about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda