1

I'm with a MEAN stack project. It's near to finish, all works, but just I would like to know if it's possible to improve the url appearance.

For example, Now I'm finding the user by the _id, and the url is showing that _id. It's possible to continue finding by id but on the front url desplaying the user name?

pogibas
  • 27,303
  • 19
  • 84
  • 117
  • Well if you know the user before navigating, you can store that user to a service and fetch it after navigating. It's hard to help further not knowing exactly what is going on in your code. – AT82 Oct 16 '17 at 05:14

1 Answers1

0

If you want to search by id, you need that id in your url. But you could have both id and userName in url (searching by id, ignoring userName), maybe that would be solution for you:

/user/:id/:userName or /user/:userName/:id

If you don't like mongo ids (they are pretty long), you could transform them e.g. via hashids or short-mongo-id module.

Or you could also add new indexed field to your users mongo table, that will have unique userName identifier, and use that instead of id field (but you will need to handle collisions in your backend).

Martin Adámek
  • 16,771
  • 5
  • 45
  • 64
  • Thank you @Martin Adámek, I'm finding well by _id, my question was if it's possible to overwrite that _id and replace it by the user name without modifying the query. So I want to continue finding by _id but showing the user name on the url. ¿is that possible through a helper service or whatever? – Pol Gasull Navarro Oct 15 '17 at 20:31
  • no its not, that what first sentence of my answer says... if you want to search by `id`, then you need to know that `id`... your frontend (angular app) have no clue about mapping of user name to id... – Martin Adámek Oct 15 '17 at 20:33
  • Thank you Martin for your time!, I will try what you said about ignoring. I thought that was possible. Just fyi (if others read the post) My question is similar than https://stackoverflow.com/questions/42533757/angular2-use-object-name-in-route-but-send-id-in-http – Pol Gasull Navarro Oct 15 '17 at 20:37