Employing the power of Bluebird's Promisifyall functionality in conjunction with M

I've implemented mongoose with Bluebird's promisifyAll like this:

var mongoose  = require('bluebird').promisifyAll(require('mongoose'))

Now, I'm trying to fetch a document from MongoDB using the "where" clause in the following code snippet:

// Retrieve list of Posts
exports.index = function(req, res) {
  console.log(req.query);
  Post.findAsync()
    .whereAsync({author: req.query.id})
    .execAsync()
    .then(function(entity) {
            if (entity) {
              res.status(statusCode).json({
                status_code: statusCode,
                data: entity
              });
            }
    })
    .catch(function(err) {
      res.status(200).json({
        status_code: statusCode,
        message: 'error occurred',
        errors: err
      });
    });
};

However, it seems to be hanging. Am I missing something here? Any assistance on utilizing promisifyAll from Bluebird along with mongoose would be greatly appreciated. Thank you :)

Answer №1

locate and determine do not function asynchronously, they do not accept callbacks. Therefore, avoid using the asynchronous variations of these methods - you are not anticipating them to return a promise, but rather a mongoose query object.

Give this a try:

Post.locate().determine({author: req.query.id}).executeAsync()
.then(…)
.…

By the way, your request will hang if element is falsy; in such cases, you do not send a response. Consider including an else statement with throw new Error("no element").

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

Integrating new components into JSON data

I started by creating a JSON document in my code using the following syntax: let jsonData = []; To populate this document, I utilized the '.push()' method to add elements in this manner: jsonData.push({type: "example", value: "123"}); Eventua ...

Selecting a chained dropdown option using jQuery in CodeIgniter

I'm struggling with jquery and ajax, particularly in implementing a select box functionality. I am using CI and below is my code. I want the data in another select box "category" to change dynamically based on the selection in the "brand" select box. ...

Issue with table display within <div> element in React

Recently diving into React, I find myself in a situation where I need to display data in a table once it's ready or show 'Loading' if not. Currently, my render() function looks like this: return ( <div> { this.state.loaded ...

How to download a dynamically generated PHP file to your local machine

I am trying to find a solution where the search results can be downloaded by the user and saved on their computer. Currently, the file is automatically stored on the server without giving the user an option to choose where to save it. In the search form, ...

Chrome's rendering of CSS flex display shows variations with identical flex items

I am facing an issue with my flex items where some of them are displaying incorrectly in Chrome but fine in Firefox. The problem seems to be with the margin-right of the rectangle span within each flex item, along with the scaling of the text span due to f ...

Using Javascript to link various arrays filled with file paths and transforming them into an object in a distinctive manner

I've tried countless times to make it work, but it just doesn't seem to work... I have an array that contains multiple other arrays: let arr = [ ["Users"], ["Users", "john"], ["Users", "john", "Desktop"], ["Users", "john", "Docum ...

Initiate an Elementor popup upon form submission (After submit event) using Contact Form 7 in WordPress

I have incorporated Elementor popups and contact form 7 into my WordPress website. Currently, my goal is to activate the Elementor popup after the contact form 7 form has been submitted. Below is what I have tried so far, and there have been no console er ...

What is the best approach to simultaneously update an array using multiple threads in a Node.js environment?

Hey there, I'm trying to figure out how to make changes to the same array using 2 worker threads in Node.js. Can anyone help me with this? My issue is that when I add a value in worker thread 1 and then try to access it in worker thread 2, the second ...

Is it advisable to combine/minimize JS/CSS that have already been minimized? If the answer is yes, what is

Our app currently relies on multiple JS library dependencies that have already been minified. We are considering consolidating them into a single file to streamline the downloading process for the browser. After exploring options like Google Closure Compi ...

When removing a class from a group of elements in JavaScript, it seems to only erase approximately fifty percent of the

I've been working on a Word Puzzle algorithm that involves displaying a set of words on a grid for users to solve. They can either attempt to solve the puzzle themselves or click a "solve" button, triggering a function to visually solve the Word Puzzl ...

Verify user access rights with Vue.js Laravel

I am currently working on a project using Laravel and Vue. In my middleware, I have the following code: public function handle($request, Closure $next) { $user = auth()->user(); if ($user->hasRole('admin')) { return ...

Validation for prototype malfunctioning

I am currently using Prototype.js to implement form validation on my website. One of the fields requires an ajax request to a PHP file for validation purposes. The PHP file will return '1' if the value is valid and '0' if it is not. Des ...

Need help with updating React component state within a Meteor.call callback?

I've constructed this jsx file that showcases a File Uploading Form: import React, { Component } from 'react'; import { Button } from 'react-bootstrap'; class UploadFile extends Component { constructor(){ super() this. ...

Troubleshooting: WordPress post not uploading to custom PHP file - Issue causing ERROR 500

Our website is built using WordPress, and I was tasked with adding a function to our newsletter subscription that sends an email to a specific address based on the selected value in the form. Everything was working perfectly on my local host, but when I tr ...

What is the best way to update a nested object in mongoose?

Just diving into mongoose, I'm looking to make changes to my city name within the address schema. Here's what my schema structure looks like: const addressSchema = new mongoose.schema({ address:{ door_no:{type:number}, other ...

Efficient methods for transferring information between a main and pop-up page within angularjs

On my webpage, I have a button that opens a popup page. I need to figure out a way to transfer json data from the main page to the popup page. These two pages are running separate angular applications. Once the data is transferred, it will be updated base ...

What are the best practices for implementing DBCursor from the mongodb java driver in spring-data-mongodb?

I am seeking the best approach to retrieve around 1 million documents from my MongoDB database using Spring Data MongoDB. Performance is a key concern, so I would prefer not to fetch all 1 million records at once. Is there a specific method within Spring D ...

Rewrite: "Rewriting URL POST requests in Node.js

Within my JavaScript file, I have this function: exports.authentication = function(req, res) { // .. // user validation // .. res.redirect('/login'); }; I'm looking to modify all POST requests to a different path, such as / ...

Puppeteer exhibiting unexpected behavior compared to the Developer Console

My goal is to extract the title of the page using Puppeteer from the following URL: Here's the code snippet I am working with: (async () => { const browser = await puppet.launch({ headless: true }); const page = a ...

What is the method for showcasing an array using AngularJs in my specific scenario?

I have an array in JavaScript structured like this: var data = [ 2005 = [ Jan = [0,1,2,3,5...], Feb = [0,1,2,3,5...], ...more ], 2006 = [ Jan = [0,1,2,3,5...], Feb = [0,1,2,3,5...], ...more ], 200 ...