Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

175
Views
Mongoose Schema Array/Object .post

I have this codes:

// Morosos.js
var mongoose = require('mongoose');  
const MorososSchema = new mongoose.Schema({
  idlor:  String,
  comunidad: String,
  vivienda:   String,
  demandado:   String,
  importe:   String,
  datos: [{ fecha: String, dato: String }],
  date: { type: Date, default: Date.now },
});
mongoose.model('Morosos', MorososSchema);
module.exports = mongoose.model('Morosos');

&&

// MorososController.js
var express = require('express');
var router = express.Router();
var bodyParser = require('body-parser');

router.use(bodyParser.urlencoded({ extended: true }));
var Morosos = require('./Morosos');

router.post('/', function (req, res) {
    console.log(req.body.datos.fecha + " " + req.body.comunidad);
    Morosos.create({
            idlor : req.body.idlor,
            comunidad : req.body.comunidad,
            vivienda : req.body.vivienda,
            demandado: req.body.demandado,
            importe: req.body.importe,
            datos:  [{fecha: req.body.datos.fecha, dato: req.body.datos.dato}] ,
            date: Date.now()
        }, 
        function (err, user) {
            if (err) return res.status(500).send("There was a problem adding the information to the database. Error: "+err);
            res.status(200).send(user);
        });
});
// RETURNS ALL THE USERS IN THE DATABASE
router.get('/', function (req, res) {
    Morosos.find({}, function (err, users) {
        if (err) return res.status(500).send("There was a problem finding the users. Error: ");
        res.status(200).send(users);
    });
});

module.exports = router;

When I use "POST" on "Postman" with x-www-form-urlencoded with this info:

idlor:LOR02/16
comunidad:XXXXX
vivienda:XXXXX
demandado:YYYYY
importe:XXXXX€
datos:{[fecha:28/09/2016,dato:Cristina]}

After trying lot of different ways I can't save the "array" datos or show the "array" datos on the server.

I put a code console.log(req.body.datos.fecha + " " + req.body.comunidad); but it throw me undefined at req.body.datos.fecha.

I'm blocked and I don't know how to solve this. Thanks!

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Try sending:

idlor:LOR02/16
comunidad:XXXXX
vivienda:XXXXX
demandado:YYYYY
importe:XXXXX€
datos[fecha]:28/09/2016
datos[dato]:Cristina
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!