2017-04-24 123 views
1

我想用arangodb在Foxx中創建一個簡單的現有微服務。我已經開始了,但是我對JavaScript很新,所以我確信這很簡單。如何將Foxx中的json對象數組發佈到arangodb

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

一)我怎麼做這個簡單的任務

b)當調試腳本

感謝的最好方式!

回答

2

修正了這個非常簡單的概念:

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

我仍然不知道如何在福克斯調試雖然

+1

我做了我的大部分調試只需使用的console.log,然後在尋找日誌菜單項的ArangoDB Web UI。這對於查看大量數據並不好,但通過Object.keys和JSON.stringify,我總是可以找出錯誤的原因。 –

+0

謝謝,我也發現之後。現在一切都很好;) – RHSMan

相關問題