2017-04-03 26 views
0

我一直在撞牆撞牆。我有一些節點代碼,拉扯了一堆googlecalendar事件,然後將這些事件映射到不同的對象結構中以供在我的Web應用程序中使用......作爲此操作的一部分,我需要將Google日曆事件中的用戶與我的Web應用程序中的用戶數據庫。下面的代碼:使用Array.map嵌套承諾,使用Promise.all,但仍然不起作用

router.post('/getCalendar', function(req,res) { 
    // authentication 
    var oauth2Client = getOAuthClient(); 
    var session = req.session; 
    oauth2Client.setCredentials({ 
    access_token: req.user.google.token, 
    refresh_token: req.user.google.refreshToken, 
    expiry_date: true 
    }) 

    var p = new Promise(function (resolve, reject) { 
    // get the next 20 calendar events 
    var calendar = google.calendar('v3'); 
     calendar.events.list({ 
     auth: oauth2Client, 
     calendarId: 'primary', 
     timeMin: (new Date()).toISOString(), 
     maxResults: 20, 
     singleEvents: true, 
     orderBy: 'startTime' 
     }, function(err, response) { 
     if (err) console.log('err',err) 
      // displays all the events 
     if (response) console.log('response',response) 
     if(response) { 
      var events = response.items; 
      resolve(events || err); 
     }   
    }); 

    }).then(function (data) { 

    var newdata = data.map(function(item) { 
     // create the new object structure and populate with some details 
     newObj = { 
      _id: item.id, 
      _title: item.summary, 
      group: [req.headers.group], 
      fields: [], 
      assignedTo: [], 
      google: "calendar", 
      googledata: item, 
      _category: [] 
     } 

     var extendedFields = {} 

     // get the list of attendees 
     attendees = item.attendees.map(function(user) { 
     var attendee = user.email; 
     return attendee; 
     }) 

     // pulls the user information for each attendee using promises 
     var promises = attendees.map(function(email) { 
     return new Promise(function(resolve, reject) { 

      // data is in a mongodb, using mongoose 
      User.findOne({'google.email': email}).exec(function (err, user) { 
      if (err) return resolve(null); 
      resolve(user); //not using reject at all so the Promise.all shouldn't fail 
      }); 
     }) 
     }) 

     Promise.all(promises) 
     .then(function(value) { 
      value.forEach(function(user) { 
      if(promise !== null) { 
       newObj.assignedTo.push(user); 
      } 
      }); 

      return newObj 

     }).catch((error) => {console.log(error)}) 

    }) 

    res.json(newdata) 
    }) 

}); 

我使用Promise.all等到所有承諾已經解決,但似乎在完成第一個之前移動到下一個項目data陣列英寸任何指導都會受到歡迎。請讓我知道是否需要更多細節。

謝謝。

+0

不要忘了'reject'時沒有響應 – Bergi

+0

您需要使用'Promise.all'和'map'爲'data'陣列以及 – Bergi

+0

@Bergi - 我想當我使用'reject'時,那麼'Promise.all'將會失敗......我也不確定如何爲你的建議使用Promise.all作爲'map'和'data'。你能提供一些更多的解釋或例子嗎? –

回答

0
  1. 返回承諾的最後一個Promise.all。

    return Promise.all(promises) 
        .then(function(value) { 
         value.forEach(function(user) { 
         // user !== null ? 
         if(promise !== null) { 
          newObj.assignedTo.push(user); 
         } 
         }); 
    
         return newObj 
        }) 
        .catch((error) => {console.log(error)}) 
    
  2. 在newdata上使用Promise.all。

    Promise.all(newdata) 
        .then(function (newObjs) { 
        // newObjs is the Array of newObj in your code 
        res.json(newObjs); 
        }) 
        .catch(function (err) { 
        console.log(err); 
        });