#javascript #node.js #react-native #express #react-native-sqlite-storage
Вопрос:
у меня есть два стола :
1 — Пользователи
Идентификатор пользователя | имя | Фамилия |
---|---|---|
1 | имя 1 | последний 1 |
2 | имя 2 | последние 2 |
2 — Книги
Идентификационная книжка | Записная книжка | Идентификатор пользователя |
---|---|---|
1 | книга 1 | 1 |
2 | книга 2 | 1 |
3 | книга 3 | 2 |
Я использую React-Native и react-native-sqlite-хранилище
listofBooks() {
return new Promise(resolve => {
var books= [];
this.initDB()
.then(db => {
db.transaction(tx => {
tx.executeSql('SELECT * from Users INNER JOIN Books ON Users.IdUser= Books.IsUser',
).then(([tx, results]) => {
var len = results.rows.length;
if (len > 0) {
for (let i = 0; i < len; i ) {
let row = results.rows.item(i);
const { IdUser, FirstName, LastName, NameBook } = row;
books.push({ IdUser, FirstName, LastName, NameBook });
}
resolve(JSON.stringify(books));
}
});
})
.then(result => {
this.closeDatabase(db);
})
.catch(err => {
console.log(err);
});
})
.catch(err => {
console.log(err);
});
});
}
функция возвращает этот json
[
{
"IdUser": 1,
"FirstName": "name1",
"LastName": "last1",
"NameBook": "book1",
},
{
"IdUser": 1,
"FirstName": "name1",
"LastName": "last1",
"NameBook": "book2",
},
{
"IdUser": 2,
"FirstName": "name2",
"LastName": "last2",
"NameBook": "book3",
}
]
but I would like my json be like that
[
{
"IdUser": 1,
"FirstName": "name1",
"LastName": "last1",
"NameBook": ["book1", "book2"],
},
{
"IdUser": 2,
"FirstName": "name2",
"LastName": "last2",
"NameBook": "book3",
}
]
is there a library for (react-native) that allows me to solve this problem easily ?
thanks for help.