A
A
Anton Ulanov2015-12-22 18:09:53
JSON
Anton Ulanov, 2015-12-22 18:09:53

How to make a json post request?

Good afternoon. I am writing a small service for a local osm server. There was a problem with a post request to add a record to the database. in this script, the uuid and the type of the label are entered, but the coordinates are not written, and I can’t figure out how to enter them there :(
Service Code:

// BASE SETUP
// =============================================================================

// call the packages we need
var express    = require('express');
var bodyParser = require('body-parser');
var app        = express();
var morgan     = require('morgan');

// configure app
app.use(morgan('dev')); // log requests to the console

// configure body parser
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

var port     = process.env.PORT || 80; // set our port

var mongoose   = require('mongoose');
mongoose.connect('mongodb://[email protected]:27017/monitor'); // connect to our database
var Device     = require('./models/device');

// ROUTES FOR OUR API
// =============================================================================

// create our router
var router = express.Router();

// middleware to use for all requests
router.use(function(req, res, next) {
  // do logging
  console.log('Something is happening.');
  next();
});

// test route to make sure everything is working (accessed at GET http://localhost:8080/api)
router.get('/', function(req, res) {
  res.render({ message: 'hooray! welcome to our api!' });	
});

// ----------------------------------------------------
router.route('/devices')


  .post(function(req, res) {
    
    var device = new Device();
    device.uuid = req.body.uuid;
    device.location.coordinates = req.body.location.coordinates;
    device.location.type = req.body.location.type;

    device.save(function(err) {
      if (err)
        res.send(err);

      res.json({ message: 'Device created!' });
    });

    
  })

  .get(function(req, res) {
    Device.find(function(err, devices) {
      if (err)
        res.send(err);

      res.json(devices);
    });
  });

router.route('/devices/:device_id')

  .get(function(req, res) {
    Device.findById(req.params.device_id, function(err, device) {
      if (err)
        res.send(err);
      res.json(device);
    });
  })

  .put(function(req, res) {
    Device.findById(req.params.device_id, function(err, device) {

      if (err)
        res.send(err);

      device.uuid = req.body.uuid;
      device.save(function(err) {
        if (err)
          res.send(err);

        res.json({ message: 'Device updated!' });
      });

    });
  })

  .delete(function(req, res) {
    Device.remove({
      _id: req.params.device_id
    }, function(err, device) {
      if (err)
        res.send(err);

      res.json({ message: 'Successfully deleted' });
    });
  });


// REGISTER OUR ROUTES -------------------------------
app.use('/api', router);

// START THE SERVER
// =============================================================================
app.listen(port);
console.log('Magic happens on port ' + port);

Model Code
// app/models/device.js

var mongoose     = require('mongoose');
var Schema       = mongoose.Schema;

var DevicesSchema   = new Schema({
    uuid: String,
    location : {
    type: { 
      type: String,
      default: 'Point'
    }, 
    coordinates: [Number]
  	}
},{
    versionKey: false
});

module.exports = mongoose.model('Device', DevicesSchema);

I would be grateful for any hints and help. Thank you all in advance

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Artem Kustikov, 2016-01-05
@art1z

Attach the JSON that comes to the POST request, it should be something like:

{
location: {
 type: 'XXX',
 coordinates: [3123, 232, 232]
}
}

A
Anton Ulanov, 2016-02-02
@antonsr98

I would be glad if you tell me how to catch it,
in the end I want to get records in the database in the form:

{
  "type": "Feature",
  "geometry": {
    "type": "Point",
    "coordinates": [125.6, 10.1]
  },
  "properties": {
    "name": "Dinagat Islands",
    "uuid": "user1"
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question