Wstęp:Dlaczego moje obiekty błędu obietnicy JS są puste?
- Korzystanie Mongoose
- Korzystanie Bluebird i zastąpienie mpromise wewnątrz Mongoose
req.helpers.consoleMessage
funkcja widać poniżej jest funkcją z jakąś prostą logikę w nim, który określa, kiedy należy, a nie w celu wyświetlenia pewien poziom szczegółowości w oparciu o istniejące debugowanie włączone w konfiguracji aplikacji ORAZ o stanie innym niż null/niezdefiniowany wyświetlanych obiektów. Całe komunikaty zostaną uszeregowane za pomocąJSON
i zwrócone do wyświetlenia na konsoli.
Kod:
Oto przykład jakiegoś kodu wykazujące te objawy.
Jest to trasa delete
dla jednostek :team
:comment
w interfejsie API, nad którym pracuję. Celowo opuściłem linię var response = user.comments;
z błędem w odwołaniu do obiektu user
, podczas gdy w rzeczywistości powinien on być team
, który powinien zostać zwrócony przez funkcję wywołującą i przekazany do obietnicy .then()
. Powinno to spowodować błąd odniesienia, ponieważ użytkownik nie jest zdefiniowany.
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);
}
Objaw:
Wywołanie tej trasy będzie produkować błąd i powodować .catch()
na ogień, starając się wyjść z błędami stanie jednak przedmiotem err
wydaje się być pusty.
Przykł. Odpowiedź HTTP: { success: false, message: {} }
Przykł. dziennik Console (skrócone dla jasności) { req: {...}, res: {...}. err: {} }
Wniosek:
Przedmiotem err
wydaje się być pusty ...
Cool! Dziękuję za udział. – danilodeveloper
Hah, popełniłem dokładnie ten sam błąd. To jest najbardziej przydatna rzecz na dzisiejszym przepełnieniu stosu. Dzięki. – jlegler