2016-07-21 24 views
5

प्रस्तावना:मेरे जेएस वादे त्रुटि वस्तुओं को खाली क्यों कर रहे हैं?

  • नेवला
  • Bluebird का उपयोग करना और नेवला के अंदर mpromise जगह
  • req.helpers.consoleMessage समारोह bellow देखा कि निर्धारित करता है जब करने के लिए और प्रदर्शित करने के लिए नहीं इसमें कुछ सरल तर्क के साथ एक समारोह है का उपयोग करना ऐप कॉन्फ़िगरेशन में डीबग के अस्तित्व के अस्तित्व के आधार पर विस्तार का एक निश्चित स्तर और प्रदर्शित होने वाली वस्तुओं की गैर-शून्य/अपरिभाषित स्थिति। पूरे संदेश JSON का उपयोग करके स्ट्रिंग किए जाते हैं और कंसोल पर प्रदर्शित होने के लिए लौटाए जाते हैं।

कोड:

यहाँ कुछ इन लक्षणों दिखा कोड का एक उदाहरण है।

यह एक एपीआई में :team:comment इकाइयों के लिए delete मार्ग है जिस पर मैं काम कर रहा हूं। मैंने जानबूझकर var response = user.comments; को user ऑब्जेक्ट के संदर्भ में एक त्रुटि के साथ लाइन छोड़ दिया है, वास्तव में यह team होना चाहिए जिसे कॉलिंग फ़ंक्शन द्वारा वापस किया जाना चाहिए और वादा .then() में भेज दिया जाना चाहिए। यह एक संदर्भ त्रुटि का कारण बनना चाहिए क्योंकि उपयोगकर्ता परिभाषित नहीं है।

var console = clim("(DELETE /api/v1/team/:team/comments/:comment):", logger); 

    // create a filters request for mongoose 
    var query = {}; 

    // determine if the :team param is a username or an object id 
    req.helpers.validateObjectId(req.db, req.params.team) ? query._id = req.params.team : query.name = req.params.team; 

    if(req.helpers.validateObjectId(req.db, req.params.comment)) { 

     // looks good; create an update object 
     var update = { $pull: { comments: { _id: req.params.comment } } }; 

     // find the comment using the query above and pull the comment id 
     req.models.Team.findOneAndUpdate(
      query, 
      update, 
      {safe: true, new : true} 
     ).then(function(team){ 

      if(!team){ 

       // create the response object 
       var response = { 
        success: false, 
        message: "Team not found" 
       }; 

       // log request 
       console.info(req.helpers.consoleMessage(req, response, null)); 

       // respond with an appropriate array 
       res.status(404).json(response); 

      }else{ 

       // create the response object using the teams's comments 
       var response = user.comments; 

       // log request 
       console.info(req.helpers.consoleMessage(req, response, null)); 

       // respond with the team comments array 
       res.status(200).json(response); 

      } 

     }).then(null, function(err){ 

      // create the response 
      var response = { success: false, message: req.config.debug ? err: "An error has occur with your request; please try again" }; 

      // log the errors 
      console.error(req.helpers.consoleMessage(req, response, err)); 

      // or send a 500 internal server error 
      res.status(500).json(response); 

     }); 

    }else{ 

     // create the response 
     var response = { success: false, message: "Comment id is not a valid object id" }; 

     // log the errors 
     console.info(req.helpers.consoleMessage(req, response, null)); 

     // or send a 500 internal server error 
     res.status(500).json(response); 

    } 

लक्षण:

इस मार्ग एक त्रुटि का उत्पादन और .catch() errored राज्य से उबरने के प्रयास में आग का कारण होगा कॉलिंग तथापि err वस्तु खाली दिखाई देता है।

पूर्व। HTTP प्रतिक्रिया: { success: false, message: {} }

पूर्व। कंसोल लॉग (स्पष्टता के लिए संक्षिप्त) { req: {...}, res: {...}. err: {} }

निष्कर्ष:

err वस्तु खाली दिखाई देता है ...

उत्तर

8

समस्या:

त्रुटि वस्तु लॉगिंग से ही सांत्वना देने यह बताएगा कि ऑब्जेक्ट वास्तव में खाली नहीं है और err.message जैसे गुणों को पकड़ना बहुत ही कामयाब है।

समस्या यह है कि जेएस त्रुटि ऑब्जेक्ट को JSON का उपयोग करके व्यापक रूप से स्ट्रिंग नहीं किया जा सकता है। यह - इस समस्या से निपटने के तरीकों के साथ - संबंधित एसओएफ प्रश्न में विस्तार से वर्णित है: Is it not possible to stringify an Error using JSON.stringify?

अपडेट किया गया कोड:

निम्नलिखित कोड में परिवर्तन संदेश निर्दिष्ट करने के लिए किए गए थे HTTP प्रतिक्रिया और सहायक समारोह (पहले वर्णित) में दिखाया जाना विशिष्ट करने के लिए अद्यतन किया गया है त्रुटि के विवरण दिखाया जा सकता है : पूर्व: { err: { message: err.message, stack: err.stack } } और इसी तरह।

var console = clim("(DELETE /api/v1/team/:team/comments/:comment):", logger); 

    // create a filters request for mongoose 
    var query = {}; 

    // determine if the :team param is a username or an object id 
    req.helpers.validateObjectId(req.db, req.params.team) ? query._id = req.params.team : query.name = req.params.team; 

    if(req.helpers.validateObjectId(req.db, req.params.comment)) { 

     // looks good; create an update object 
     var update = { $pull: { comments: { _id: req.params.comment } } }; 

     // find the comment using the query above and pull the comment id 
     req.models.Team.findOneAndUpdate(
      query, 
      update, 
      {safe: true, new : true} 
     ).then(function(team){ 

      if(!team){ 

       // create the response object 
       var response = { 
        success: false, 
        message: "Team not found" 
       }; 

       // log request 
       console.info(req.helpers.consoleMessage(req, response, null)); 

       // respond with an appropriate array 
       res.status(404).json(response); 

      }else{ 

       // create the response object using the teams's comments 
       var response = team.comments; 

       // log request 
       console.info(req.helpers.consoleMessage(req, response, null)); 

       // respond with the team comments array 
       res.status(200).json(response); 

      } 

     }).then(null, function(err){ 

      // create the response 
      var response = { success: false, message: req.config.debug ? err.message : "An error has occur with your request; please try again" }; 

      // log the errors 
      console.error(req.helpers.consoleMessage(req, response, err)); 

      // or send a 500 internal server error 
      res.status(500).json(response); 

     }); 

    }else{ 

     // create the response 
     var response = { success: false, message: "Comment id is not a valid object id" }; 

     // log the errors 
     console.info(req.helpers.consoleMessage(req, response, null)); 

     // or send a 500 internal server error 
     res.status(500).json(response); 

    } 

मैं ऐसी सरल अवधारणा क्यों साझा कर रहा हूं?

मैं यह पता लगाने की क्या मैं अपने वादे श्रृंखला संरचनाओं के साथ गलत तरीके से कर रहा था की कोशिश कर रहा घंटे के लिए खोज की है और कीवर्ड empty और error (जे एस वादे के बारे में शब्दों के प्रत्येक संयोजन के साथ) थे, लेकिन अपनी खोजों में से कोई भी कुछ भी साथ आया था यह पुष्टि करने के अलावा उपयोगी है कि मैं इसे सही तरीके से देख रहा था। सबकुछ मेरी सहायक लिपि के साथ ठीक दिख रहा था और मैं यह समझने के लिए सही डीबगिंग कदम नहीं कर सका कि समस्या तब तक थी जब तक कि err ऑब्जेक्ट को सीधे कंसोल में आउटपुट करने की कोशिश करने पर समस्या नहीं थी (मुझे ऐसा करने की आवश्यकता क्यों होगी मैं पहले से ही था ... या तो मैंने सोचा)।

तो मुझे लगता है कि आप कह सकते हैं कि मैं कुछ लोगों को कुछ समय बचाने की कोशिश कर रहा हूं यदि कोई भी ऐसी स्थिति में चलता है और सोच रहा है "मेरे वादे इरादे से क्यों काम नहीं कर रहे हैं!" और, मेरे जैसे, गलत दिशा में खोजना होता है।

हैप्पी कोडिंग!

+0

कूल! शेयर के लिए धन्यवाद। – danilodeveloper

+2

हां, मैंने वही गलती की है। आज मेरे लिए स्टैक ओवरफ्लो पर यह सबसे उपयोगी बात है। धन्यवाद। – jlegler

संबंधित मुद्दे

 संबंधित मुद्दे