Tabnine Logo For Javascript
Model
Code IndexAdd Tabnine to your IDE (free)

How to use
Model
in
mongoose

Best JavaScript code snippets using mongoose.Model(Showing top 15 results out of 9,027)

origin: builderbook/builderbook

router.get('/books', async (req, res) => {
 try {
  const books = await Book.list();
  res.json(books);
 } catch (err) {
  res.json({ error: err.message || err.toString() });
 }
});
origin: builderbook/builderbook

static async getBySlug({ slug }) {
  const bookDoc = await this.findOne({ slug });
  if (!bookDoc) {
   throw new Error('Book not found');
  }

  const book = bookDoc.toObject();

  book.chapters = (await Chapter.find({ bookId: book._id }, 'title slug').sort({ order: 1 })).map(
   (chapter) => chapter.toObject(),
  );
  return book;
 }
origin: builderbook/builderbook

router.get('/books/:slug', async (req, res) => {
 try {
  const book = await Book.getBySlug({ slug: req.params.slug, userId: req.user && req.user.id });
  res.json(book);
 } catch (err) {
  res.json({ error: err.message || err.toString() });
 }
});
origin: builderbook/builderbook

router.post('/books/edit', async (req, res) => {
 try {
  const editedBook = await Book.edit(req.body);
  res.json(editedBook);
 } catch (err) {
  res.json({ error: err.message || err.toString() });
 }
});
origin: builderbook/builderbook

async function getEmailTemplate(name, params) {
 const source = await EmailTemplate.findOne({ name });
 if (!source) {
  throw new Error(`No EmailTemplates found.
   Please check that at least one is generated at server startup,
   restart your server and try again.`);
 }

 return {
  message: _.template(source.message)(params),
  subject: _.template(source.subject)(params),
 };
}
origin: builderbook/builderbook

router.get('/books/detail/:slug', async (req, res) => {
 try {
  const book = await Book.getBySlug({ slug: req.params.slug });
  res.json(book);
 } catch (err) {
  res.json({ error: err.message || err.toString() });
 }
});
origin: builderbook/builderbook

router.get('/my-books', async (req, res) => {
 try {
  const { purchasedBookIds = [] } = req.user;

  const { purchasedBooks } = await Book.getPurchasedBooks({ purchasedBookIds });

  res.json({ purchasedBooks });
 } catch (err) {
  res.json({ error: err.message || err.toString() });
 }
});
origin: builderbook/builderbook

router.post('/books/edit', async (req, res) => {
 try {
  const editedBook = await Book.edit(req.body);
  res.json(editedBook);
 } catch (err) {
  res.json({ error: err.message || err.toString() });
 }
});
origin: builderbook/builderbook

router.get('/books', async (req, res) => {
 try {
  const books = await Book.list();
  res.json(books);
 } catch (err) {
  res.json({ error: err.message || err.toString() });
 }
});
origin: builderbook/builderbook

async function getEmailTemplate(name, params) {
 const source = await EmailTemplate.findOne({ name });
 if (!source) {
  throw new Error(`No EmailTemplates found.
   Please check that at least one is generated at server startup,
   restart your server and try again.`);
 }

 return {
  message: _.template(source.message)(params),
  subject: _.template(source.subject)(params),
 };
}
origin: builderbook/builderbook

static async getBySlug({ slug }) {
  const bookDoc = await this.findOne({ slug });
  if (!bookDoc) {
   throw new Error('Book not found');
  }

  const book = bookDoc.toObject();

  book.chapters = (await Chapter.find({ bookId: book._id }, 'title slug').sort({ order: 1 })).map(
   (chapter) => chapter.toObject(),
  );
  return book;
 }
origin: builderbook/builderbook

async function getEmailTemplate(name, params) {
 const source = await EmailTemplate.findOne({ name });
 if (!source) {
  throw new Error(`No EmailTemplates found.
   Please check that at least one is generated at server startup,
   restart your server and try again.`);
 }

 return {
  message: _.template(source.message)(params),
  subject: _.template(source.subject)(params),
 };
}
origin: builderbook/builderbook

router.post('/books/edit', async (req, res) => {
 try {
  await Book.edit(req.body);
  res.json({ done: 1 });
 } catch (err) {
  res.json({ error: err.message || err.toString() });
 }
});
origin: builderbook/builderbook

router.get('/books', async (req, res) => {
 try {
  const books = await Book.list();
  res.json(books);
 } catch (err) {
  res.json({ error: err.message || err.toString() });
 }
});
origin: builderbook/builderbook

async function getEmailTemplate(name, params) {
 const source = await EmailTemplate.findOne({ name });
 if (!source) {
  throw new Error(`No EmailTemplates found.
   Please check that at least one is generated at server startup,
   restart your server and try again.`);
 }

 return {
  message: _.template(source.message)(params),
  subject: _.template(source.subject)(params),
 };
}
mongoose(npm)Model

Most used mongoose functions

  • Model.find
    Finds documents.
  • Model.findOne
    Finds one document.
  • connect
  • Model.findById
    Finds a single document by its _id field. findById(id) is almost*
  • connection
  • DocumentQuery.then,
  • Model.create,
  • DocumentQuery.exec,
  • Model.remove,
  • Schema.pre,
  • Model.findByIdAndUpdate,
  • next,
  • Connection.once,
  • DocumentQuery.sort,
  • Model.findOneAndUpdate,
  • DocumentQuery.populate,
  • Model.findByIdAndRemove,
  • Model.update,
  • DocumentQuery.limit

Popular in JavaScript

  • qs
    A querystring parser that supports nesting and arrays, with a depth limit
  • redis
    Redis client library
  • bluebird
    Full featured Promises/A+ implementation with exceptionally good performance
  • ws
    Simple to use, blazing fast and thoroughly tested websocket client and server for Node.js
  • yargs
    yargs the modern, pirate-themed, successor to optimist.
  • node-fetch
    A light-weight module that brings window.fetch to node.js
  • express
    Fast, unopinionated, minimalist web framework
  • through2
    A tiny wrapper around Node.js streams.Transform (Streams2/3) to avoid explicit subclassing noise
  • readable-stream
    Streams3, a user-land copy of the stream library from Node.js
  • Top 25 Plugins for Webstorm
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimAtomGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyStudentsTerms of usePrivacy policyJavascript Code Index
Get Tabnine for your IDE now