Junior Christ
Junior Christ

Reputation: 167

Why the data is not being completly sent to mongodb database?

I am doing a basic CRUD operation using Angular, when I try to add a new person in the angular form the data is sent to the server but at the of sending it to the mongodb database, it only show the _id in the databse but no the rest of the data, I dont understand why the data is not being completly sent, who can help me on this, this is my code:

var express = require('express');
const app = express();
var bodyParser = require('body-parser');
var path = require('path');
var mongo = require('mongoose');
var cors = require('cors');
const personasRoutes = express.Router();

var multer = require('multer');
var xlstojson = require("xls-to-json-lc");
var xlsxtojson = require("xlsx-to-json-lc");
const fs = require('fs');

app.use(bodyParser());
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:true}));

//DATABASE

var db = mongo.connect("mongodb://localhost:27017/test", { useNewUrlParser: 
true }, function(err, response){
if(err){
    console.log(err)
}else{
    console.log('Conectado a ' + db);
}
})

//MONGOOSE

var Schema = mongo.Schema;

var PersonasSchema = new Schema({
cedula: {type: String},
direccion: {type: String},
nombre: {type: String}
}, { versionKey: false });

var model = mongo.model('Persona', PersonasSchema);

app.post("/guardarUsuario", function(req, res){
   console.log("Dentro!!!!");
   console.log(req.body);
   var mod = new model(req.body);
   mod.save()
    .then(persona => {
      console.log("Los datos fueron guardados!");
   })
   .catch(err => {
      console.log("No se pudieron guardar los datos!");
  });
});

app.listen('3000', function(){
  console.log('running on 3000 port');
})

This is the ANGULAR code: personas.service.ts

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Injectable({
providedIn: 'root'
})
export class PersonasService {

uri = 'http://localhost:3000';

constructor(private http: HttpClient) { }

addPersona(PersonaCedula, PersonaDireccion, PersonaNombre) {
  const obj = {
    PersonaCedula,
    PersonaDireccion,
    PersonaNombre
  };
  console.log(obj);
  this.http.post(`${this.uri}/guardarUsuario`, obj)
  // this.http.post(`${this.uri}/agregar`, obj)
  .subscribe(res => console.log('done'));
  }
 }

This is the result of the Database: Mongodb database

Upvotes: 1

Views: 85

Answers (1)

PeS
PeS

Reputation: 4039

I believe your properties of your object that is sent from client do not match those in the schema. Actually, what does that console.log(req.body); show in the log?

Try:

addPersona(PersonaCedula, PersonaDireccion, PersonaNombre) {
  const obj = {
    cedula: PersonaCedula,
    direccion: PersonaDireccion,
    nombre: PersonaNombre
  };
  console.log(obj);
  this.http.post(`${this.uri}/guardarUsuario`, obj)
  // this.http.post(`${this.uri}/agregar`, obj)
  .subscribe(res => console.log('done'));
  }
 }

Upvotes: 1

Related Questions