Witam,
mam mały problem z sortowaniem parent komentarzy, MongoDB wyświetla poprawne rezultaty,
ale po przejściu przez filtr zwraca mi tylko "strictMode" [3] z trzema komentarzami, zamiast wszystkich.
console.log [1] zwraca poprawny wynik, ośmiu komentarzy, jeżeli dodam zwrócony wynik z konsoli [2] na twardo to poprawnie wyświetla posortowane komentarze.
po dodaniu lean()
[4] wyświetla same trzy komentarze bez strictMode.
//StrictMode jest defaultowo wyłączony, nie aktywowałem go.
Baza działa poprawnie, zwraca poprawny wynik, tak samo funkcja sortująca nest() działa poprawnie ale tylko z statycznymi danymi, ale gdy baza wysyła dane do funkcji nest() zostaje wyświetlony strictMode.
Comment.find({ posturl: url }, function (err, allPostComments) {
const nest = (items, _id = null) =>
items
.filter(item => item.subcomment === _id)
.map(item => ({ ...item, children: nest(items, item._id) }));
console.log(allPostComments); // [1]
const nestedComments = nest(allPostComments);
response.status(200).json({ status: "success", comments: nestedComments });
});
// }).lean(); [4]
// to jest poprawnie wyświetlane i sortowane gdy dodam na twardo.
// [2]
const allPostComments = [{
subcomment: null,
_id: '5eb2db3a394e0610b83984e6',
posturl: '952a763f4a9f31c715da5a74fb19477a',
comment: 'comment 1',
__v: 0
},
{
subcomment: null,
_id: '5eb2db47394e0610b83984e7',
posturl: '952a763f4a9f31c715da5a74fb19477a',
comment: 'comment 2',
__v: 0
},
{
subcomment: '5eb2db47394e0610b83984e7',
_id: '5eb2db64394e0610b83984e8',
posturl: '952a763f4a9f31c715da5a74fb19477a',
comment: 'subcomment 3',
__v: 0
},
{
subcomment: '5eb2db47394e0610b83984e7',
_id: '5eb2db74394e0610b83984e9',
posturl: '952a763f4a9f31c715da5a74fb19477a',
comment: 'subcomment 4',
__v: 0
}
]; // ...
// [3]
[
{
'$__': InternalCache {
strictMode: true,
selected: {},
shardval: undefined,
saveError: undefined,
validationError: undefined,
adhocPaths: undefined,
removing: undefined,
inserting: undefined,
saving: undefined,
version: undefined,
getters: {},
_id: 5eb2db3a394e0610b83984e6,
populate: undefined,
populated: undefined,
wasPopulated: false,
scope: undefined,
activePaths: [StateMachine],
pathsToScopes: {},
cachedRequired: {},
session: null,
'$setCalled': Set {},
ownerDocument: undefined,
fullPath: undefined,
emitter: [EventEmitter],
'$options': [Object]
},
isNew: false,
...