Skip to content

Instantly share code, notes, and snippets.

@justsml
Forked from Tinusw/myController.js
Last active August 29, 2018 02:43
Show Gist options
  • Save justsml/a18928a9f1305d8b09570a8906a868eb to your computer and use it in GitHub Desktop.
Save justsml/a18928a9f1305d8b09570a8906a868eb to your computer and use it in GitHub Desktop.

Revisions

  1. justsml revised this gist Aug 29, 2018. 1 changed file with 21 additions and 18 deletions.
    39 changes: 21 additions & 18 deletions myController.js
    Original file line number Diff line number Diff line change
    @@ -5,28 +5,31 @@ const StoreB = mongoose.model('StoreB');
    // Tells package to use es6 promises
    mongoose.Promise = global.Promise;

    exports.createStore = (req, res) => {
    exports.createStore = (req, res, next) => {
    const store = new Store(req.body);
    const record_we_want_to_associate
    const itemB = StoreB
    .findOne({ id: req.params._id})
    .catch(err => {
    throw Error(err);
    });
    // Tighter Express Pattern:
    .catch(next);
    // Standard re-throw, technically not needed here*
    // .catch(err => {
    // throw Error(err); // Needs `new`, or no stack trace is created,
    // });

    store.storeb_id = record_we_want_to_associate._id
    // This line wont work, record_we_want_to_associate is a promise, not a StoreB
    // store.storeb_id = record_we_want_to_associate._id

    store
    .save()
    // after save return all records
    .then(stores => {
    return Store.find()
    // For a dependent task, we access `record_we_want_to_associate`/itemB with `.then()`:
    itemB
    .then(record => {
    store.storeb_id = record._id;

    return store.save()
    // after save return all records
    .then(stores => Store.find())
    // then display them on index page
    .then(stores => res.render('index', {stores: stores}))
    })
    // then display them on index page
    .then(stores => {
    res.render('index', {stores: stores}
    }
    // deal with errors
    .catch(err => {
    throw Error(err)
    }
    .catch(next);
    }
  2. @Tinusw Tinusw revised this gist Mar 13, 2018. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions myController.js
    Original file line number Diff line number Diff line change
    @@ -7,13 +7,13 @@ mongoose.Promise = global.Promise;

    exports.createStore = (req, res) => {
    const store = new Store(req.body);
    let association
    const record_we_want_to_associate
    .findOne({ id: req.params._id})
    .catch(err => {
    throw Error(err);
    });

    store.storeb_id = association._id
    store.storeb_id = record_we_want_to_associate._id

    store
    .save()
  3. @Tinusw Tinusw created this gist Mar 13, 2018.
    32 changes: 32 additions & 0 deletions myController.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    const mongoose = require('mongoose');
    const Store = mongoose.model('Store');
    const StoreB = mongoose.model('StoreB');

    // Tells package to use es6 promises
    mongoose.Promise = global.Promise;

    exports.createStore = (req, res) => {
    const store = new Store(req.body);
    let association
    .findOne({ id: req.params._id})
    .catch(err => {
    throw Error(err);
    });

    store.storeb_id = association._id

    store
    .save()
    // after save return all records
    .then(stores => {
    return Store.find()
    })
    // then display them on index page
    .then(stores => {
    res.render('index', {stores: stores}
    }
    // deal with errors
    .catch(err => {
    throw Error(err)
    }
    }