Этот кусок кода работал и вдруг перестал работать. Не могу понять в чём причина
router.post('/', function (req, res, next) {
email.find({email: req.body.email}, function(err, data) {
if(data[0].email === req.body.email) {
res.send('Email exist!');
} else {
var dbData = new email({email: req.body.email});
dbData.save(function (err, dbData, affected) {
res.send(dbData);
console.log(dbData);
});
}
});
});
Приложение падает с такими логами
app-0 (entire): TypeError: Cannot read property 'email' of undefined
app-0 (entire): at saveEmail (/var/www/app/routes/index.js:20:19)
app-0 (entire): at Promise.<anonymous> (/var/www/app/routes/index.js:32:9)
app-0 (entire): at Promise.<anonymous> (/var/www/app/node_modules/mongoose/node_modules/mpromise/lib/promise.js:177:8)
app-0 (entire): at Promise.emit (events.js:95:17)
app-0 (entire): at Promise.emit (/var/www/app/node_modules/mongoose/node_modules/mpromise/lib/promise.js:84:38)
app-0 (entire): at Promise.fulfill (/var/www/app/node_modules/mongoose/node_modules/mpromise/lib/promise.js:97:20)
app-0 (entire): at Object.cb (/var/www/app/node_modules/mongoose/lib/query.js:1010:22)
app-0 (entire): at Object._onImmediate (/var/www/app/node_modules/mongoose/node_modules/mquery/lib/utils.js:137:16)
app-0 (entire): at processImmediate [as _immediateCallback] (timers.js:354:15)
UPD
Делайте лишние проверки
Логи ругались на несуществующее свойство
email, только я не туда смотрел
email.find({email: req.body.email}, function(err, data) {
if(data.length !== 0 && data[0].email === req.body.email) {
res.send('Email exist!');
} else {
var dbData = new email({email: req.body.email});
dbData.save(function (err, dbData, affected) {
res.send(dbData);
console.log(dbData);
});
}
});