Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion packages/lib-js-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
"test": "npm run test:unit && npm run test:e2e",
"test:base": "cross-env NODE_ENV=test mocha --reporter spec --require babel-register",
"test:unit": "npm run test:base -- test/unit/**/*.js",
"test:e2e": "npm run test:base -- --timeout 30000 --slow 8000 test/e2e/*.js",
"lint": "standard --fix --env mocha",
"format": "prettier --write --single-quote --no-semi --no-bracket-spacing *.js {src,test}/**/*.js"
},
Expand Down
6 changes: 4 additions & 2 deletions packages/lib-js-core/src/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,10 +272,12 @@ class Data extends QueryBuilder {
const itemFieldKey = key.split('.').shift()
const itemField = get(item, itemFieldKey)

if (Array.isArray(itemField)) {
if (Array.isArray(itemField) && typeof itemField[0] === 'object' && itemField[0] !== null) {
itemField.forEach((arrItem, i) => {
const path = `${itemFieldKey}.[${i}].${key.split('.').slice(1)}`
set(all, path, get(item, path))
const mappedPath = `${itemFieldKey}.[${i}].${(fields[key] || key).split('.').slice(1)}`

set(all, mappedPath, get(item, path))
})
} else {
set(all, fields[key] || key, get(item, key))
Expand Down
37 changes: 37 additions & 0 deletions packages/lib-js-core/test/unit/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,43 @@ describe('Data', () => {
.should.become([{author: 'John', views: 100}])
})

it('should work with nested array of objects', () => {
api
.get(`/v2/instances/${instanceName}/classes/users/objects/`)
.reply(200, {objects: [
{
name: 'John',
views: 100,
id: 2,
tags: ['css', 'html'],
documents: [
{
id: 10,
name: 'Test document'
}
]
}
]})

return data.users
.fields([
'name as author',
'views',
'tags',
'documents.id',
'documents.name as documents.title'
])
.list()
.should.become([{
tags: ['css', 'html'],
documents: [
{id: 10, title: 'Test document'}
],
author: 'John',
views: 100
}])
})

it('should work with create method', () => {
api
.post(`/v2/instances/${instanceName}/classes/posts/objects/`)
Expand Down