Итак, я создал обработчик маршрута для некоторых опросов, которые отправляются клиентам с экземпляром опроса:
module.exports = app => {
app.post('/api/surveys', requireLogin, requireCredits, async (req, res) => {
const { title, subject, body, recipients } = req.body;
const survey = new Survey({
title,
subject,
body,
recipients: recipients.split(',').map(email => ({ email: email.trim() })),
_user: req.user.id,
dateSent: Date.now()
});
// Great place to send an email!
const mailer = new Mailer(survey, surveyTemplate(survey));
try {
await mailer.send();
await survey.save();
req.user.credits -= 1;
const user = await req.user.save();
res.send(user);
} catch (err) {
res.status(422).send(err);
}
});
};
У меня только разработан бэкэнд, поэтому мне пришлось перейти на фронт Reactзакончите и добавьте axios
к моему window
объекту так:
// Development only axios helpers - do not push to production!
import axios from 'axios';
window.axios = axios;
И затем создайте объект опроса в консоли:
const survey = { title: 'my title', subject: 'Give Us Feedback' , recipients: 'renaissance.scholar2012@gmail.com', body: 'We would love to hear if you enjoyed our services' };
undefined
survey
{title: "my title", subject: "Give Us Feedback", recipients: "renaissance.scholar2012@gmail.com", body: "We would love to hear if you enjoyed our services"}
axios.post('/api/surveys', survey);
Promise {<pending>}
Письмо с опросом было успешно отправлено, но если вы посмотрите на экземпляр survey
, а затем посмотрите на коллекцию:
> db.surveys.find()
{ "_id" : ObjectId("5bc1579ec759e774e1bdf253"), "yes" : 0, "no" : 0, "title" : "my title", "subject" : "Give Us Feedback", "body" : "We would love to hear if you enjoyed our services", "recipients" : [ { "responded" : false, "_id" : ObjectId("5bc1579ec759e774e1bdf254"), "email" : "renaissance.scholar2012@gmail.com" } ], "_user" : ObjectId("5ad25c401dfbaee22188a93b"), "__v" : 0 }
>
Отсутствует dateSent
:
dateSent: Date.now()
Я работаю mongod
локальнои просмотр этого из mongo
оболочки.Если бы я сделал это в MLab, появились бы dateSent
и _user
?Есть ли разница?Не уверен, почему я не получаю эти свойства в коллекции.