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

225
Vistas
NextJS fetching nested array of objects from MongoDB using mongoose and getServerSideProps

I'm trying to fetch an array of objects from MongoDB, using mongoose and SSP. One hitch is that all ObjectIds must be converted into strings. Currently I'm doing it like this:

export async function getServerSideProps({ query }) {
    
  try {
    const { user } = query
    await connectDB()
    const currentUser = await User.findOne({ user }).lean(),
      { _id } = await currentUser,
      userProperties = await Property.find({ ownerId: _id }).lean()
    
    currentUser._id = currentUser._id.toString()
        
    userProperties.forEach(props => {
      props._id = props._id.toString()
      props.ownerId = props.ownerId.toString()
      props.subarray.forEach(props => {
      props._id = props._id.toString()
    })
  })
        
  if (!currentUser) {
    return {
      notFound: true
      }
    }
    
    return {
      props: {
        currentUser,
        userProperties
      }
    }
  } catch (err) {
    console.log(err)
    
    return {
      redirect: {
        destination: '/',
        statusCode: 307
      }
    }
  }
}

This produces: Error: If(...): Nothing was returned from render. I can fetch user without properties, no propblem, and I can console log the attached properties even though nothing is returned. What's happening here?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

In order to send an array from getServerSideProps(), you should first transform it to a series of bytes with JSON.stringify, remember that HTTP sends text only. Re-transform as an array of objects in your React component with JSON.parse, with this method, you don't need to create the strings manually.

about 4 years ago · Juan Pablo Isaza Denunciar
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