Note that there are some explanatory texts on larger screens.

plurals
  1. POProper separation of concerns natively in node.js?
    primarykey
    data
    text
    <p>I am a total node.js noobie and trying to figure out the best way to structure my application with proper separation of concerns.</p> <p>I am using mongodb via mongoose and have successfully gotten my controllers separated out using node.js modules and am trying to then separate out my models. What I've gone appears to work, but when I check the database nothing has been saved. Also, I tried a <code>console.log()</code> in the save function and nothing gets logged.</p> <p>from my server.js I have:</p> <pre><code>app.post(api.urlslug + '/messages', messagesapi.insert); </code></pre> <p>I then have a /controllers/api/messages.js:</p> <pre><code>var m = require('../../models/message'); exports.index = function(req, res, next){ res.send('all the messages...'); } exports.insert = function(req, res, next){ var message; message = new m.Message({ messagebody: req.body.messagebody }); message.save(function(err) { console.log('here we are in the save'); if(!err) { return console.log('message created'); } else { return console.log(err); } }); return res.send(message); } </code></pre> <p>and my /models/message.js looks like this:</p> <pre><code>// required modules var mongoose = require('mongoose') , db = require('../models/db'); // setup database connection mongoose.connect(db.connectionstring()); var Message = exports.Message = mongoose.model('Message', new mongoose.Schema({ messagebody: String })); </code></pre> <p>When I post to the API I get a the proper JSON back and it even has the _id field with what appears to me as a mongodb provided unique id. With that, I am having trouble understanding why it is not actually going into mongodb if it appears to be creating the object and communicating with mongodb correctly.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload