En ImageSchema , tengo algunos campos: title , imageURL de imagen. Cuando recupero datos de una sola imagen, quiero agregar http://localhost:4000/ a imageURL.
"imageURL": "http://localhost:4000/uploads/photo-1508919801845.jpeg",
Datos de ejemplo:
{ "_id": "612e0328c1c6dd25c6f14fd4", "title": "photo-1508919801845", "imageURL": "/uploads/photo-1508919801845.jpeg", "createdAt": "2021-08-31T10:23:36.419Z", "updatedAt": "2021-08-31T10:23:36.419Z", "__v": 0 }Controlador.ts
const imageId = req.params.id const findImage = await Image.findOne({ _id: imageId}) if (!findImage) { return res.status(404).json({error: true, msg: "Image not found"}) } const image = await Image.findById({_id: imageId}).select({ _id: 0, __v: 0}) return res.status(200).json({ error: false, data: image })¿Cómo puedo hacer eso?
Puede usar la canalización de Aggregation con el operador $concat :
Image.aggregate([ { "$match": { "_id": "612e0328c1c6dd25c6f14fd4" } }, { "$set": { "imageURL": { "$concat": [ "http://localhost:4000", "$imageURL" ] } } } ])