“Mongoose mendapatkan dokumen” Kode Jawaban

Mongoose mendapatkan dokumen

//Router file. After doing all your imports at beginning of file
const router = express.Router();
app.use('/pathforpostrequests', router);

router.get('/:name', controller.getPerson, (req, res, next) => {
  res.status(200).json(res.locals.person);
});

// Controller file. After doing all your imports at beginning of file. Person is an example mongo schema you will need to setup in a schema file.
const controller = {
  getPerson (req, res, next) {
    Person.findOne({ firstName: req.params.name }).exec()
    .then((result) => {
    res.locals.person = result;
    next();
    })
  },
}
felinehasher

findbyid mongoose

// Find the adventure with the given `id`, or `null` if not found
await Adventure.findById(id).exec();

// using callback
Adventure.findById(id, function (err, adventure) {});

// select only the adventures name and length
await Adventure.findById(id, 'name length').exec();
Cheerful Cockroach

Jawaban yang mirip dengan “Mongoose mendapatkan dokumen”

Pertanyaan yang mirip dengan “Mongoose mendapatkan dokumen”

Lebih banyak jawaban terkait untuk “Mongoose mendapatkan dokumen” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya