为什么数据没有完全发送到mongodb数据库?

时间:2019-07-02 23:31:35

标签: node.js angular mongodb

我正在使用Angular执行基本的CRUD操作,当我尝试以角度形式添加新人员时,数据被发送到服务器,但是在将其发送到mongodb数据库时,它仅显示{{1 }}在数据库中,但没有其余的数据,我不明白为什么数据没有被完整发送,谁能帮我解决这个问题,这是我的代码:

_id

这是英语代码: personas.service.ts

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');
})

这是数据库的结果: Mongodb database

1 个答案:

答案 0 :(得分:1)

我相信从客户端发送的对象的属性与架构中的属性不匹配。实际上,console.log(req.body);在日志中显示什么?

尝试:

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'));
  }
 }