Tuesday, 4 April 2017

Node Mongoose - Querying data results in "pending" promise

I'm in the process of creating a simple node script that queries a Mongo Database, based on a Mongoose query and for whatever reason, I can't get the promise to resolve where I can actually work with the data.

Here is my node script:

#!/usr/bin/env node

const WorkOrderController = require('../controllers/work-order');
var mongoose = require('mongoose');
mongoose.Promise = global.Promise;

let promise = WorkOrderController.getNeedsScheduledOrders();
console.log('promise', promise);

Here is the exported function from my Work Order schema controller:

exports.getNeedsScheduledOrders = (req, res, next) => {

  WorkOrder.find().
    where('quote.is_quoted').equals(true).
    where('schedule.is_scheduled').equals(false).
    where('is_archived').equals(false).
    where('on_hold').equals(false).
    populate('customer_id', 'name').
    populate('department_id', 'title').
    //populate('quote', 'hours').
    select('_id department_id customer_id quote').
    exec(function(err, workorders) {
      res.status(200).json({
        workorders
      });
      return next();
    })
}

No matter what I try, when I try to console.log the promise, it gives me undefined. I know the query itself works because it's being utilized inside of React app.

I'm new to Mongoose as I inherited this project from another developer. Anyone know what needs to be done here?



via Stevie Star

No comments:

Post a Comment