Home > Enterprise >  Node.JS / Mongo does not save complete Data
Node.JS / Mongo does not save complete Data

Time:11-24

I created a first Schema and another one (2 tables) in MongoDB to house 2 separate information. Now the first works fine, without challenge , but the second schema is supposed to house user information.

Now i have a problem with getting user information. I dont seem to understand what the problem is.

The Schema Looks like this

var db = require('../database');
var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var SubscriptionSchema = new Schema({
    company_name : String,
    company_address : String,
    company_city : String,
    company_state : String,
    companyrep_name : String,
    companyrep_email : String,
    company_telnum : String,
    company_zip : String,
    company_website : String,
    timezone : String,
    company_logo : String,
    company_country : String,
    product_requested : String,
    methodof_payment : String,
    dateof_request : String,
    dateof_expiry : String,
});

var endUserRegisterSchema = new Schema({
    username : String,
    company_name : String,
    password : String,
    fullname : String,
    company_ccy: String,
    company_timezone : String    
})

module.exports = mongoose.model('Subscription',SubscriptionSchema);
module.exports = mongoose.model('Users',endUserRegisterSchema);

Then adding it for routes it is supposed to be looking like this

The router in users.js which is supposed to save the information is Looking like this

router.post('/', function (req, res) {
  var newReg = new Users();

  newReg.username = req.body.username;
  newReg.company_name = req.body.company_name;
  newReg.password = req.body.password;
  newReg.fullname = req.body.fullname;

  newReg.save(function(err,Users){
    if(err){
      res.send('Error registering User');
    }else{
      res.send(Users);
    }
  });
});

Then on app.js i added the corresponding URL

to browse the REST api. All these work, but I have a problem, it does not save the informtion completely to Mongo DB. When I pass as JSON like this

{
  "username":"admin@********.com",
  "company_name":"blah blah blah",
  "password":"supermna1",
  "fullname":"Admin_blah blah"
}

I get this back as Response , rather than the full data

{
    "_id": "619ddde9ff437222b17e888d",
    "company_name": "blah blah blah",
    "__v": 0
}

Is there something i am not getting right? I would be needing some form of Clarification here

CodePudding user response:

Separating into smaller bits worked for me and all is good now. Whatever Schema one is creating, split into separate schemas. Thats what I did and everything is fine on this End.

CodePudding user response:

Try doing this instead

router.post('/', async function(req, res) {

  try {
    var newReg = new Users();

    newReg.username = req.body.username;
    newReg.company_name = req.body.company_name;
    newReg.password = req.body.password;
    newReg.fullname = req.body.fullname;

    await newReg.save();
    res.send(newReg);
  } catch (err) {
    res.send('Error registering User');
  }
});
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related