Retrieving data from mongodb individually

I have a database table called Questions.

My goal is to display the first question and then upon user interaction, show them the next one.

To retrieve the first question, I use the following query:

Template.home.user_questions = function () {
 return Questions.find({}, {sort: {answer1:{'$ne': ''}}, limit: 1});
}

Handling the user's click event is done as follows:

'click input' : function () {
  Questions.update(this._id, {$inc: {value1: 1}})
  // now display the next question
}

However, I am struggling with showing the next question as Meteor does not support hasNext() and next() methods.

Answer №1

If you need to skip over questions, you can utilize the skip method.

Template.home.user_questions = function () {
    var skip = Session.get("skip") || 0;
    return Questions.find({}, {sort: {answer1:{'$ne': ''}}, limit: 1, skip: skip});
}

To progress to the next question, simply increase the value of the session variable skip by 1. For example:

Session.set("skip", (Session.get("skip") || 0) ++);

By doing this, the system will automatically move on to the subsequent question, proceeding until the final one is reached.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Issue with MVC Bundle not functioning properly in Release Configuration due to Debug being set to False, causing CSS and JS files to not

Whenever I deploy my MVC.Net application in release mode, the following configuration will be present in the web.config file: <compilation debug="false" targetFramework="4.5" /> After making the above changes, the site loads with CSS and JavaScript ...

Is there a way to retrieve all documents based on the start and end of a specific day?

One common issue I am facing involves submitting a date to my nodejs server in a specific format

 2018-11-02T00:36:00+05:30 // The actual time should be 12:36AM However, when examining the document in my database (using studio 3T), the format appear ...

Concealing a column in ngx-datatable Ionic

I am just starting to work with NGX-datatable and I need to populate my data table in my Ionic app with data from Firebase. Here is the format of the JSON returned from Firebase: If I choose not to use the User_id as a column, how can I access the User_id ...

Switch between showing and hiding a div by clicking on an image

Currently, I am experimenting with the toggle div function and utilizing images as triggers for toggling. For instance, when a div is closed, an image of a "plus" sign indicates to the user that it can be expanded, and vice versa for compressing the div. T ...

I. Discovering the Step-by-Step Guide on Retrieving User Information from Facebook Upon Generating App

I have set up a Facebook login for user registration on my website. However, I am only able to retrieve the profile name and user ID from Facebook. How can I access the email and other user information? Here is the data I am currently receiving from Faceb ...

Tips for unit testing an Angular Service that is primarily responsible for redirecting to an external page from the application

My service is responsible for redirecting to a separate login page since we are implementing login as a service. function redirectToMembership() { var returnURL = $location.host(); returnURL+="/#/Authorization"; $window.location.href=Environme ...

Retrieving specific properties in the aggregate output from a MongoDB document

I'm struggling to figure out how to use aggregate in order to retrieve very specific types of results. The format of my database documents is as follows: { _id: 122434 houseDetails: { hasBedroom: { flag: true, }, hasKitchen: ...

When an option that is already selected in AngularJS is clicked, the ng-model value will be updated

I currently have a select element with a data-ng-options attribute: <select data-ng-model='myValue'> <option value=''> Default </option> <option data-ng-repeat="i in item" value='{$ i.value $}'& ...

When using Multer for file upload, the req.body returns as an empty object while req.file is undefined

I previously shared a problem I encountered while using multer for file upload in the MERN stack. Despite my attempts, I have not yet been able to resolve it. In my app, I am using both body-parser and multer. Here is the order of code in my index.js file: ...

Troubles encountered with the search bar filter functionality, implementing JS/JQuery within a Laravel blade template

Currently, I have a blade template containing a search bar for filtering purposes. The issue I'm encountering is that the filtering functionality doesn't seem to work as expected after removing Angular from the page entirely. The page is set up ...

Having trouble retrieving subdocuments with my query - seeking assistance

As a newcomer to mongoose, I am currently working on creating a query that specifically retrieves a subdocument from my main document. The structure of my document is as follows: PROFILE - CONTACTS [ARRAY] I have developed an endpoint for fetching a ...

Experiencing problems with CSS compatibility while transitioning from vanilla JavaScript to React

Currently, I am working on revamping the frontend of an app that was initially built with vanilla javascript and transitioning it to a React framework. However, I'm encountering some challenges with styling the HTML elements. Specifically, the textare ...

What are the steps for sending a file?

I am looking to send an image to my telegram bot using JavaScript, without the use of Node.js. To achieve this, I require the token of the bot and my Telegram user ID. While sending text messages is successful, I have also managed to send photos by provid ...

JavaScript | Determining the coordinates of where on the image X and Y were clicked

My goal is to determine the position of an x and y location when a user clicks on an image. This information will be used to add a spot to the image that displays relevant information. The content for the spot will be dynamically loaded from a SQL database ...

Expecting expression and a syntax error occurred: JSX nextjs token was unexpected

When rendering a table from an array, I utilized the following code: const Posts: NextPage = ({ posts}: any) => { return ( <> ..... <tbody> { posts.map((post: any) => { const _date = new ...

JavaScript issue with confirm/redirect feature not functioning as expected

A demonstration of JavaScript is utilized for deleting an employee in this scenario... <script type="text/javascript"> function deleteEmployee(employee) { var confirmation = confirm('Are you sure?'); if(confirmation) { ...

Error encountered while using Chart.js with JSON dataset

Struggling to make this work... Here are the necessary scripts: <script src="Chart.js"></script> <script src="jquery-1.11.3.min.js"></script> This is the full code I am working with: <body> <div id="chartCanvas"> &l ...

Having trouble utilizing a function with an async onload method within a service in Angular - why does the same function work flawlessly in a component?

I successfully created a component in Angular that can import an Excel file, convert it into an array, and display its content as a table on the page. The current implementation within the component looks like this: data-import.compoent.ts import { Compo ...

Creating a dual-direction infinite scroll effect with CSS through mouse dragging

I'm currently working on implementing an infinite scroll component for a project. After consulting this tutorial, I've encountered an issue. It seems that I can only achieve infinite scroll in one direction. Whenever I add elements to the leftmo ...

Having issues with IE8 and SmoothGallery script errors

I'm currently utilizing the SmoothGallery plugin created by Jon Designs to enhance the website of one of my clients. However, there seems to be a minor issue in Internet Explorer 8 when attempting to navigate to the next image. Interestingly enough, a ...