0

After posting in db.json a new record (transaction with several data), I would like to get the id of the new record before going on. How-to do it ? I am stuck

I have in my db.json a "table" positions with an id automatically generated

my request is

request({
    url: url + '/api/positions,
    method: 'POST',
    form: transaction
}, ????);

I want in my callback retrieve position.id (as I have to use it in another part of code).

Thanks a lot

Olivier

olivier
  • 1
  • 4

2 Answers2

0

Make sure your API is giving back the position id, Im assuming you're using express, so in /api/positions have the response be res.send({position...});

EDIT, So you're asking about routing? Here is an awesome guide: https://expressjs.com/en/guide/routing.html but Ill give you an example of what I mean.

app.post('/api/positions', function(req, res) { 
  //your logic goes here, so you are sending a 'transaction' object 
  // npm install and use body parser so you can retrieve attributes 
  // from the body of the post
  res.send({position: x}) 
})
ACalza
  • 96
  • 1
  • 5
  • could you just give me an example (it is one of my first dev with node.js, express and request) as I just want to get the last record – olivier Oct 14 '16 at 19:24
  • Sorry I don't understand, I am lost – olivier Oct 14 '16 at 20:00
  • Maybe I am not clear and too junior !!! I have in my table "positions" { "productCode": "FUT1", "id": 1 }, { "productCode": "FUT2", "id": 2 }, When I post my transaction with request POST + FORM, I know that I will have { "productCode": "FUT3", "id": 3 }, in positions. I want to update my other object transaction with position.id = 3, I need this result in the callback function in order to amend transaction with transaction.positionId = 3 – olivier Oct 14 '16 at 20:04
  • Can you show me your code? Yorur route code to be exact? – ACalza Oct 14 '16 at 20:14
  • var request = require('request'); var util = require('util'); var port = process.env.PORT || 3000; var url = 'http://127.0.0.1:' + port ; var urlPositions = url + '/api/positions'; var urlTransactions = url + '/api/transactions'; – olivier Oct 14 '16 at 20:17
  • Is that all the code? I mean the code where you set up the routing to make the POST/GET requests. – ACalza Oct 14 '16 at 20:22
  • function Position_New(transaction, res) { function processResponse(error, res, body) { if (error) { throw new Error(error); } else{} } var posDate = new Date().toISOString().split('T')[0]; util.log('Create a new position'); transaction.posDate = posDate; request({ url: urlPositions, method: 'POST', form: transaction }, 'mycallback'); } – olivier Oct 14 '16 at 20:24
  • HOw come you aren't using express? That's the only way you can get the server working. You don't even have a server running. – ACalza Oct 14 '16 at 20:36
  • Even if I am not using Express, it seems that with request, I can update my db.json, but I must confess that I am learning by doing... I do not want to waste your time but it would be great if you could just draft me in one simple comprehensive example how to do? – olivier Oct 14 '16 at 20:41
  • https://expressjs.com/en/starter/hello-world.html in the url, go to port localhost:3000/ and youll see hello world. – ACalza Oct 15 '16 at 01:46
0

Maybe some thing like that.

...
var router = jsonServer.router('db.json');
...

// 'router.db.get' will fetch any first level attribute from your db.json
var positions = router.db.get('positions').value();
var lastPosition = positions[positions.length-1];
Camille
  • 2,439
  • 1
  • 14
  • 32