प्रस्तावना:मेरे जेएस वादे त्रुटि वस्तुओं को खाली क्यों कर रहे हैं?
- नेवला
- 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
वस्तु खाली दिखाई देता है ...
कूल! शेयर के लिए धन्यवाद। – danilodeveloper
हां, मैंने वही गलती की है। आज मेरे लिए स्टैक ओवरफ्लो पर यह सबसे उपयोगी बात है। धन्यवाद। – jlegler