1

I am trying to create a simple existing microservice in Foxx with arangodb. I have followed the getting started but I am pretty new to javascript so I am sure this is very simple.

const db = require('@arangodb').db;
const errors = require('@arangodb').errors;
const foxxColl = db._collection('myCollection');
const DOC_NOT_FOUND = errors.ERROR_ARANGO_DOCUMENT_NOT_FOUND.code;

router.post('/create_entry', function (req, res) {
const data = req.body;
const meta = foxxColl.save(req.body);
res.send(Object.assign(data, meta));
})
.body(joi.object().required(), 'Entry to store in the collection.')
.response(joi.object().required(), 'Entry stored in the collection.')
.summary('Store an entry')
.description('Stores an entry in the "initial_balance" collection.');

This obviously is good. I would however like to load a bulk payload

[
 {"key1": "value1", "key2": "valueA"},
 {"key1": "value2", "key2": "valueB"},
 {"key1": "value3", "key2": "valueC"}
]

I have this which fails (internal server error).

 const initSchmea = joi.object().keys({user_id:joi.string().required(),amount: joi.number().required()});

router.post('/initial_balance/bulk', function (req, res) {
    var data = req.body.;
    for(var i in data) 
      {
        var res = foxxColl.save(d[i]);
      }
    res.send('Done')

})
.body(joi.array().items(initSchmea.required()), 'Entry to store in the collection.')
.response(['text/plain'], 'Entries stored in the collection.')
.summary('Store entries')
.description('Stores entries in the "initial_balance" collection.');

a) how do I do this simple task

b) what is the best way to debug the scripts

Thanks!

RHSMan
  • 157
  • 2
  • 15

1 Answers1

2

Fixed with this very simple concept:

router.post('/create_entries', function (req, res) {
    var data = req.body;
    for(var i = 0; i < data.length; i++) {
        var obj = data[i];
        var res = foxxColl.save(obj);   
        }

})
.body(joi.array().items(joi.object().unknown(true)), ['json'])
//.response(['text/plain'], 'Entries stored in the collection.')
.summary('Store entries')
.description('Stores entries in the "initial_balance" collection.');

I am still not sure how to debug in Foxx though

RHSMan
  • 157
  • 2
  • 15
  • 1
    I do most of my debugging just by using console.log and then looking in the LOGS menu entry of the ArangoDB Web UI. It's not good for viewing large amounts of data, but with `Object.keys` and `JSON.stringify` I can always work out what was going wrong. – David Thomas Apr 25 '17 at 01:26