Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Creating Data with POST | Section
Working with MongoDB in Express Applications

bookCreating Data with POST

Svep för att visa menyn

To store data in MongoDB, you create a new document using a model.

This is usually done in a POST route.

app.post('/users', async (req, res) => {
  const user = new User(req.body);

  const savedUser = await user.save();

  res.json(savedUser);
});

The data comes from req.body. A new instance of the model is created and then saved to the database.

After saving, the stored document is returned as a response.

Example request body:

{ "name": "John", "age": 25 }

This creates a new record in the database.

question mark

What does user.save() do?

Vänligen välj det korrekta svaret

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 1. Kapitel 7

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

Avsnitt 1. Kapitel 7
some-alt