0

I'm new to Express/Node/Mongo and trying to build an assets database application to store fixed assets.

I'm trying to save the fixed asset data through the web form but only the ID is being created for some reason and the actual data from the form is not saving.

I've looked at the data in my mongo container and can only see an ID for each asset I've created

Here is my route...

/* POST create fixed-assets page. */
router.post('/financial/assets/create-fixed-asset', secured(), function(req, res, next) {

  var assetData = {
    model_number: req.body.model_number,
    manufacturer: req.body.manufacturer,
    description: req.body.description,
    serial_number: req.body.serial_number,
    asset_tag_number: req.body.asset_tag_number,
    condition_when_purchased: req.body.condition_when_purchased,
    price_paid: req.body.price_paid
  };
  FixedAsset.create(assetData, function (error, asset) {
    if (error) {
      return next(error);
    } else {
      res.redirect('/financial/assets/fixed-assets');
    }
  });

});

Here is my list view...(using Pug/Jade)

block view
  .animated.fadeIn
    h1 Fixed Assets

    a(href='create-fixed-asset/') Create
    br
    br
    table#example.display
      thead
        tr
          th ID
          th Model
          th Description
      tbody
        each asset in assets
          tr
            td #{asset.id}
            td #{asset.model_number}
            td #{asset.manufacturer}

Here is my mongoose model...

var mongoose = require('mongoose');
var FixedAssetSchema = new mongoose.Schema({
  model_number: {
    type: String
  },
  manufacturer: {
    type: String
  },
  description: {
    type: String
  },
  serial_number: {
    type: String
  },
  asset_tag_number: {
    type: Number
  },
  condition_when_purchased: {
    type: String
  },
  price_paid: {
    type: Number
  }
});

var FixedAsset = mongoose.model('FixedAsset', FixedAssetSchema);
module.exports = FixedAsset;

Does anyone see why this is happening? Thanks

Edit: Also I forgot to put the code for my Pug form. Here it is...

extends /default

block scripts
  if !starter
    // Plugins and scripts required by this view
    script(src='/js/main.js')


block view
  .animated.fadeIn
    .container.row
      .col-md-6
        h1 #{title}
    .container.row
      .col-md-6
        form(method='POST' action='/financial/assets/create-fixed-asset')
          div.form-group
            input#model_number.form-control(type='text', placeholder='Model Number')
          div.form-group
            input#manufacturer.form-control(type='text', placeholder='Manufacturer')
          div.form-group
            input#serial_number.form-control(type='text', placeholder='Serial Number')
          div.form-group
            input#description.form-control(type='text', placeholder='Description')
          div.form-group
            input#asset_tag_number.form-control(type='text', placeholder='Asset Tag Number')
          div.form-group
            input#condition_when_purchased.form-control(type='text', placeholder='Condition When Purchased')
          div.form-group
            input#price_paid.form-control(type='text', placeholder='Price Paid')
          br
          button.btn.btn-success(type='submit') Create
  • What is `secured()`? And more importantly are you sure your `req.body` actually contains the data you need? – molamk Feb 11 '19 at 21:51
  • secured() is for securing routes with Auth0. I'm not sure req.body does contain the data I need, is there a way to check? –  Feb 11 '19 at 21:57
  • 1
    yes, you can use the Node debugger. Or just `console.log(req.body)`. If it doesn't contain the data, then that's the problem – molamk Feb 11 '19 at 22:02
  • Thanks, so I changed the res.redirect('/financial/assets/fixed-assets'); to console.log(req.body); in my route and I'm getting back an empty json object when I submit –  Feb 11 '19 at 22:12
  • 1
    You don't need to replace `res.redirect`. Just add the `console.log` in the beginning. What does the `console.log` say? – molamk Feb 11 '19 at 22:18
  • this is what i get in the console when I submit the form - `database | {}` `database | POST /financial/assets/create-fixed-asset 302 115.263 ms - 104` database is my docker container –  Feb 11 '19 at 22:23

2 Answers2

0

My recommendation is to use an asynchronous route and await the creation of the fixed asset:

router.post('/financial/assets/create-fixed-asset', secured(),async function(req, res, next) {
  try{
    var assetData = {
      model_number: req.body.model_number,
      manufacturer: req.body.manufacturer,
      description: req.body.description,
      serial_number: req.body.serial_number,
      asset_tag_number: req.body.asset_tag_number,
      condition_when_purchased: req.body.condition_when_purchased,
      price_paid: req.body.price_paid
    };
    await FixedAsset.create(assetData, function (error, asset) {
      if (error) {
        return next(error);
      } else {
        res.redirect('/financial/assets/fixed-assets');
      }
    });
  }
  catch(err){
    res.redirect('/somewhere else/ a 404 page')
  }
});
Colin Daniel
  • 180
  • 10
  • I tried this but I'm still getting a blank object created –  Feb 12 '19 at 02:20
  • what happens when you console.log(req.body)? I haven't used Pug/Jade before so I don't know how to make requests through those types of views but make sure you're using [bodyparser](https://www.npmjs.com/package/body-parser) in your server/app/main.js file. Other than that, your route looks good. – Colin Daniel Feb 12 '19 at 02:58
  • If I `console.log(req.body)` I get `{}`. - Just an empty object. In my app.js file I have `var bodyParser = require('body-parser'); app.use(bodyParser.urlencoded({ extended: false }));` I'm thinking maybe it is a problem with my Pug. I forgot to add my pug form - I just added it in an edit of my original post –  Feb 12 '19 at 23:07
  • extended should be true because you can't nest objects when it's false. [Here](https://stackoverflow.com/questions/29960764/what-does-extended-mean-in-express-4-0) – Colin Daniel Feb 12 '19 at 23:32
0

I figured it out. It was my Pug form. I had forgot to put the name attribute in my form fields. Noob mistake. Thanks for your guys' help