【问题标题】:How to post an array of json objects in Foxx to arangodb如何将 Foxx 中的 json 对象数组发布到 arangodb
【发布时间】:2017-04-24 23:25:34
【问题描述】:

我正在尝试使用 arangodb 在 Foxx 中创建一个简单的现有微服务。我已按照入门指南进行操作,但我对 javascript 还很陌生,所以我确信这很简单。

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

这显然是好的。但是,我想加载批量有效负载

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

我有这个失败(内部服务器错误)。

 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) 我该如何做这个简单的任务

b) 调试脚本的最佳方法是什么

谢谢!

【问题讨论】:

    标签: javascript arangodb foxx


    【解决方案1】:

    修正了这个非常简单的概念:

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

    我仍然不确定如何在 Foxx 中进行调试

    【讨论】:

    • 我的大部分调试工作都是通过使用 console.log 然后查看 ArangoDB Web UI 的 LOGS 菜单条目来完成的。它不适合查看大量数据,但使用 Object.keysJSON.stringify 我总能找出问题所在。
    猜你喜欢
    • 2020-07-19
    • 2015-03-22
    • 1970-01-01
    • 1970-01-01
    • 2019-12-06
    • 2017-10-31
    • 2011-06-14
    • 1970-01-01
    • 2012-12-21
    相关资源
    最近更新 更多