Hello, i my project i have defined a schema as below:
As show without specify the id field.
Then when i want to create a new record its displays as
Which it is correct and it is what i want, including the id field.
The problem is that i want to do some staff on the post route and i need to get that id of the new record created, but when i get the request.body.data, inside the attributes fields the id field does not appear, so how can I get it?
Thanks!
Hi @usr1vang,
The id of mongo documents are generated by the database.
What are you trying to achieve with the id after a save?
It sounds you are making a route override.
In this case the id will be available at this place.
The documentation about route override is here
Regards
Yes, but the problem is that when i console log the request.body it not appear the id created.
The request does not contain the id, that’s normal.
It’s because the id is created by the database, so it’s contained only in the response.
It sounds you are making a route override, is it true?
In this case the id will be available at this place, on the server.
Regards
Ok. Yes, i am making that when creating a record on some collections also save data on another collections. Thats working, the problem it is i need that id created by mongoose on the first collections. I understood that it is contained in the response, but i can not get it. How can i fetch that id on the server?
Can you please describe how things should be if it worked?
This is my code.
There where i create a new event, it is comment the reference_id since it is where the id of the newly created template would go.
What I want to achieve is right there, in that path which creates the new portfolio template, get the id of that new portfolio template which is assigned by mongoose and pass it to the reference_id field of the event.
Hello,
Please find here the mongoose documentation about save
method.
Here is an extract of an example:
product.sold = Date.now();
product = await product.save();
// product.id exists
The save method will return the saved document, including the id created during the save process.
Here is another extract describing the result of the save
method:
Returns:
«Promise,undefined» Returns undefined if used with callback or a Promise otherwise
Since you are working with async/await
it’s preferrable to not pass any callback and prefer the same code as in the example below.
Regards
Hi, thanks for your time. Maybe it gets a bit consfuse with the other code i add on de post request. Lets start again. I have the portfolio template post request as it comes with the initial forest admin configuration, i mean:
There when it creates a new portfolio template, it adds the id field created by mongoose. What i want to achive is, inside this route, get that id that mongoose create for that specific record. Lets says i want to console.log that id that it is created by mongoose.
It is possible to get it? how could I get it?
Thanks!
Yes, it’s possible.
The thing is you need to save by yourself the document so you can access the result.
So you have to replace the next
L.26 by your own save code (example available from the link in my previous comment)
Then you can access to the result of the save method, with the id.
Regards
ok, now it is clearer.
Anyway I still have problems because next() makes that after creating the new record it redirects to the details of that new record. I wrote this code below where I get the id as I wanted and save the new data in the db, but I would be missing the redirection to the details of this new record. Could you help me with some code?
code:
let newPortfolioTemplate = new portfoliotemplates(
request.body.data.attributes
);
console.log(newPortfolioTemplate._id);
newPortfolioTemplate = await newPortfolioTemplate.save();
I still can’t get the job done, I would appreciate if you could help me with some code.
Thanks!
@usr1vang,
We do provide examples on our documentation here that should give a complete example of the default POST
route behavior. Sending back to the frontend a similar response will do the redirection you are expecting.
Let me know if that helps