Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

182
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda