Query MongoDB using an OpenWhisk action

Having trouble creating an OpenWhisk action that executes a find query in MongoDB and returns the results? No worries, as I am having difficulty getting it to work correctly. Even though I've connected OpenWhisk with MongoDB properly, I'm still not receiving any results. Can someone provide a code example to help me out?

Here's what my current code looks like:

function main(){
var MongoClient = require('mongodb').MongoClient
var url = 'mongodb://192.168.1.14:27017/'

    MongoClient.connect(url, (err, db) => {
        db.db('yelp').collection('Review').find({stars:5}).limit(100).toArray().then((docs) => {
            return docs;
            db.close();            
        }).catch((err) => {
            console.log(err.stack);
        });
    })
}

Unfortunately, I'm only getting null as the result. Any suggestions or tips on how to fix this issue would be greatly appreciated!

Answer №1

To handle asynchronous tasks, it is essential to utilize tools like Promises or async/await and ensure that the action execution waits until a response is received from the database.

For guidance on implementing promises with MongoDB client in Node.js, refer to this question: How to use MongoDB with promises in Node.js?

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

Triggering a click event in React when a link is clicked

I noticed something strange with a component I created that is quite simple. import React, {Component} from 'react'; const Logo = ({onClick}) => { return ( <a name="home" onClick={onClick} className="logo"> <span classN ...

Update a specific section of the web panel within Genexus at regular intervals, such as every 10 seconds

I am working on a simple web panel with a notifications icon. I need the icon to refresh every x seconds to check for any unread notifications in the database. I have tried using web notifications in Genexus, but without success. Can anyone suggest how to ...

Attempting to conditionally map an array of objects

I am currently working on conditionally rendering content on a screen. My main task involves manipulating an array of 3 objects with sub-objects, stored as the initial state in my reducer (using dummy data). The layout includes a customized SideNav bar wit ...

Tips for transferring value between custom elements in Polymer 1.0

I have developed two unique custom elements named student-details.html and student-service.html This is how the student-details.html file is structured: <link rel="import" href="../bower_components/polymer/polymer.html"> <link rel="import" hre ...

"Implementing a 2D graphical user interface on a three.js

Struggling to create a 2D graphical user interface on the renderer. The challenge lies in positioning the 2D GUI dynamically based on the width of an element drawn in threejs (the element has a width of X using threejs units, and the menu needs to be posit ...

Verifying the presence of a popover

Utilizing bootstrap popover in my project, I am encountering an issue where the target variable may not always exist on the page. Currently, I am displaying the popover using the following code snippet - target = $('#' + currentPopoverId.data(& ...

Explore one of the elements within a tuple

Can we simplify mapping a tuple element in TypeScript? I'm seeking an elegant way to abstract the following task const arr: [string, string][] = [['a', 'b'], ['c', 'd'], ['e', 'f']] const f ...

Solving automatically generated TypeScript MongoDB types for GraphQL results

Utilizing the typescript-mongodb plugin along with graphql-codegen to automatically generate Typescript types enables easy data retrieval from MongoDB and GraphQL output via Node. The initial input schema in GraphQL format appears as follows: type User @ ...

Retrieving the value of the selected radio button

How do I access the value of a clicked radio button? I am trying to create a custom filter based on the gender selected in my <form>. However, when using the filter and orderBy in my ng-repeat, it is not functioning correctly. You can view an examp ...

What is the best way to invoke a function within a controller from a .factory service?

I have been working on a code snippet where I am trying to create a generic function. This function, when given the name of a function in my controller, should be run from a factory. app.factory('myfactory', function () { return { cre ...

What is the importance of accessing the session object prior to the saving and setting of a cookie by Express-Session?

Quick Summary: Why is it crucial to access the session object? app.use((req, res, next) => { req.session.init = "init"; next(); }); ...in the app.js file after implementing the session middleware for it to properly function? Neglecti ...

What is the process for resetting a search filter list box?

I developed a search feature for filtering a list based on the instructions provided in this W3Schools tutorial: function myFunction() { var input, filter, ul, li, a, i, txtValue; input = document.getElementById("myInput"); filter = input.va ...

Displaying [object Object] in Angular Material datatable

I am currently working on implementing a datatable component using Express and Firebase DB. Below is the service request data: getText() { return this.http.get<nomchamp[]>(this.url) .map(res => { console.log(res); return res }); ...

Unable to add a review to the user profile using Node.js (Express) and MongoDB (Mongoose)

I am encountering an issue when it comes to adding reviews for a user. After creating a review for a user, only the ID is displayed, but not the text or author or any other properties. I would appreciate any insights on this matter. Below are my schemas a ...

utilizing a modal to trigger a function within the parent scope of an Angular application

I have a main.controller.js where there is a modal in place to notify the user of a warning before proceeding to the rest of the application. For example, I want to trigger my fileNew function from the modal. In main.controller.js: $scope.warningMod ...

React/MUI Popover not aligning properly due to anchorPosition issue

I'm currently implementing a React/MUI Popover within a List element from react-window. However, I'm facing an issue with positioning the Popover correctly, as it always ends up in the top left corner of the window. This occurs because the compon ...

What is the best way to incorporate Vuetify into a new application?

Recently, I developed a new app using Vue and I decided to integrate Vuetify as the framework. After executing the npm install vuetify --save command, Vuetify was added to the app successfully. However, when I tried to use it, the CSS button colors were no ...

Conceal rows in a table that are not selected using Jquery when the table has been loaded from a given URL

Below is the HTML code for an user search input field and the results table: <div class="input-group"> <input type="text" id="q" class="form-control" placeholder="Search for User"> <span class="input-group-btn"> ...

Jquery onchange event fails to fire

$('#selectclassid').trigger("change"); $('#selectclassid').on('change', function() { }); When trying to manually trigger the onchange event, it does not seem to be firing as expected. Although this format is commonly seen ...

jQuery is not defined when importing reactjs, bootstrap, and npm modules

Having trouble using Bootstrap in my React app, as I keep getting an error message saying Error: Bootstrap's JavaScript requires jQuery. I've already imported jQuery and tried various solutions found in other posts like: import $ from 'jque ...