Any idea why wix-data's next() is only skipping half the limit?

Hi people!
I’m trying to use the wix-data API to query the Blog/Posts database.
However, I’m unable to use the next() function properly. According to the function’s description, it is supposed to skip the pageSize set by limit().
This is the code I’m using:


if ( postResults == undefined ) {
let categoryID = getCategoryID ( category );

return  wixData . query ( "Blog/Posts" ) 
	. hasSome ( "categories" ,  **await**  categoryID ) 
	. limit ( ***length*** ) 
	. find () 
	. then (( results ) => { 
		postResults  =  results ; 
		return  results . items ; 
	}); 

}

if ( direction == “up” ) {
if (! postResults . hasNext ())
return null ;

return  postResults . next (). then (( results ) => { 
	postResults  =  results ; 
	return  results . items ; 
}); 

}

if (! postResults . hasPrev ())
return null ;

return postResults . prev (). then (( results ) => {
postResults = results ;
return results . items ;
});


length is set to 6 and stays that way; yet when I call next() instead of getting elements 6-11, I get elements 3-8.

I can do this manually with skip and an updated variable but I shouldn’t need to?

Any idea what’s going on?

I don’t know if your promise timing is correct.
Maybe it gets the results of the .prev() before the .next() and overrides the first results. You should carefully handle the promises.

Hi J.D.,

Thanks for the response but I don’t think that’s the case as I would expect to see null in the console log from the hasPrev().

Anyway you should handle the promises because the way you did it (assuming that you posted the code as is) is incorrect. But I guess you only posted some parts of the code which makes it hard to understand.

@jonatandor35 Hi, doing some further tests, even if I add an ‘else’ after the if (direction == “up”) check (now if ( direction == directionEnum . NEXT ) ) it still skips 3 rather than 6.


export async function getPostData ( category , offset , length ) {
if ( postResults == undefined ) {
let categoryID = getCategoryID ( category );

	return  wixData . query ( "Blog/Posts" ) 
		. hasSome ( "categories" ,  **await**  categoryID ) 
		. limit ( length ) 
		. find () 
		. then (( results ) => { 
            		postResults  =  results ; 
			return  results . items ; 
		}); 
} 

**if**  ( direction  ==  directionEnum . NEXT ) { 
	**if**  (! postResults . hasNext ()) 
		return  **null** ; 

	return  postResults . next (). then (( results ) => { 
		postResults  =  results ; 
		return  results . items ; 
	}); 
}  **else**  { 
	**if**  (! postResults . hasPrev ()) 
		return  **null** ; 

	return  postResults . prev (). then (( results ) => { 
		postResults  =  results ; 
		return  results . items ; 
	}); 
} 

}


I am also curious by what you mean when you say my implementation of Promises is wrong. I’m a self taught JS man so any advice would be appreciated :slight_smile: