简体   繁体   中英

Function result not accessible outside of scope

I have a function that works and returns data correctly to a console.log . How do I then wrap this function up and call on it, retrieving the data as needed? I have tried the below with no luck.

All this code works:

function weekendPlans() {
  Entry.aggregate(
      [
          { "$redact": {
              "$cond": {
                  "if": {
                      "$or": [
                          { "$eq": [ { "$dayOfWeek": "$selectedDate" }, 1 ] },
                          { "$eq": [ { "$dayOfWeek": "$selectedDate" }, 6 ] },
                          { "$eq": [ { "$dayOfWeek": "$selectedDate" }, 7 ] }
                      ]
                  },
                  "then": "$$KEEP",
                  "else": "$$PRUNE"
              }
          }}
      ],
      // GET THE RESULTS AND RETURN IF selectedDate MATCHES THIS WEEKEND
      function(err,results) {
        if (err) throw err;
        //console.log(results);
        var i = results.length;
        var theWeekend;

        while(i--) {
          if(results[i].selectedDate === friday || saturday || sunday) {
              theWeekend = results[i];
              break;
          }
        }
        console.log(theWeekend);
      }
)};

Calling the function outside of scope returns undefined

console.log(weekendPlans());

Anticipated result:

{ _id: 56fe9fe71f84acc2564b9fe8,
  url: 'http://www.timeoutshanghai.com/features/Blog-Food__Drink/35271/Baristas-showcase-latte-art-in-Shanghai.html',
  title: 'TIMEOUT',
  selectedDate: Sat Apr 02 2016 01:00:00 GMT+0100 (BST),
  __v: 0 }

Because this is an asynchronous operation, you'll need to rethink the way you implement functions. Borrowing from Node's event-driven model, add a callback to it:

function weekendPlans(callback) {
                      // ^ this is the magic param
  Entry.aggregate(
      [
          { "$redact": {
              "$cond": {
                  "if": {
                      "$or": [
                          { "$eq": [ { "$dayOfWeek": "$selectedDate" }, 1 ] },
                          { "$eq": [ { "$dayOfWeek": "$selectedDate" }, 6 ] },
                          { "$eq": [ { "$dayOfWeek": "$selectedDate" }, 7 ] }
                      ]
                  },
                  "then": "$$KEEP",
                  "else": "$$PRUNE"
              }
          }}
      ],
      // GET THE RESULTS AND RETURN IF selectedDate MATCHES THIS WEEKEND
      function(err,results) {
        // if (err) throw err;
        // we don't need to throw an error here, we'll pass it in the cb function
        //console.log(results);
        var i = results.length;
        var theWeekend;

        while(i--) {
          if(results[i].selectedDate === friday || saturday || sunday) {
              theWeekend = results[i];
              break;
          }
        }
        callback(err, theWeekend)
        // ^ call the callback
      }
)};

Then use it like so:

weekendPlans(function(err, theWeekend) {
    if (err) throw err
    // now you can check for err and reference theWeekend
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM