【发布时间】:2020-01-13 05:01:58
【问题描述】:
如何在 mongoose js 中插入日期时间?,我知道日期时间是 Schema 中的日期。
这是我的架构:
const mongoose = require('mongoose');
const { Schema } = mongoose;
const todoSchema = new Schema({
name: String,
description: String,
isDone: Boolean,
createdAt: Date,
updatedAt: Date
});
mongoose.model('todos', todoSchema);
这是我的发帖路线:
app.post('/api/todos', async (req, res) => {
const { name, description, isDone,createdAt,updatedAt} = req.body;
const todo = new Todo({
name,
description,
isDone,
createdAt,
updatedAt
});
try {
let newTodo = await todo.save();
res.status(201).send(newTodo);
} catch (err) {
if (err.name === 'MongoError') {
res.status(409).send(err.message);
}
res.status(500).send(err);
}
});
这是我的日期时间选择器输入:
import React from 'react';
import DateTimePicker from 'react-widgets/lib/DateTimePicker';
import Moment from 'moment';
import momentLocalizer from 'react-widgets-moment';
Moment.locale('en');
momentLocalizer();
const DateTimePickerInput = ({
label,
input,
format,
width,
placeholder,
defaultValue,
meta: { touched, error },
}) => {
return (
<div className='form-group'>
<label forname={input.name}>{label}</label> <br />
<DateTimePicker
placeholderText={placeholder}
className='form-control'
/>
<div className='text-danger' style={{ marginBottom: '20px' }}>
{touched && error}
</div>
</div>
);
};
export default DateTimePickerInput;
这是我的带有日期时间选择器字段的待办事项表单:
<Field
name='createdAt'
component={DateTimePickerInput}
dateFormat='dd-MM-yyyy'
// dateFormat='dd-MM-yyyy H:mm'
// showTimeSelect
// timeFormat='HH:mm'
placeholder='todoCreatedAt...'
label='CreatedAt'
/>
<Field
name='updatedAt'
component={DateTimePickerInput}
dateFormat='dd-MM-yyyy'
// dateFormat='dd-MM-yyyy H:mm'
// showTimeSelect
//timeFormat='HH:mm'
placeholder='todoUpdatedAt...'
label='UpdatedAt'
/>
如何在数据库中插入日期时间,在这种情况下,createdAt 和 updatedAt。也许我做错了邮寄路线。 我希望 createdAt 和 updatedAt 保存在数据库中,但实际输出这些数据并未保存在数据库中。
怎么了?
【问题讨论】: