问题
I'm trying to save datetime values in TodoForm but those values are not reflected in database. This is my todo model.
Todo.js
const mongoose = require('mongoose');
const { Schema } = mongoose;
const todoSchema = new Schema({
name: String,
description: String,
isDone: Boolean,
createdAt: Date,
updatedAt: Date
},
{timestamps: {createdAt: 'created_at' updatedAt: 'updated_at'}}
);
mongoose.model('todos', todoSchema);
Then Follows my todosController post route.
TodosController:
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);
}
});
That is from backend side. Code is next is from front end side.
TodoForm.jsx
componentWillReceiveProps = nextProps => {
// Load Contact Asynchronously
const { todo } = nextProps;
if (todo._id !== this.props.todo._id) {
// Initialize form only once
this.props.initialize(todo);
this.isUpdating = true;
}
};
render() {
const { handleSubmit, loading } = this.props;
if (loading) {
return <span>Loading...</span>;
}
return (
<form onSubmit={handleSubmit}>
<Field
name='createdAt'
component={DateTimePickerInput}
dateFormat='dd-MM-yyyy'
// dateFormat='dd-MM-yyyy H:mm'
// showTimeSelect
timeFormat='HH:mm'
placeholder=' todo createdAt...'
label='CreatedAt'
/>
<Field
name='updatedAt'
component={DateTimePickerInput}
dateFormat='dd-MM-yyyy'
// dateFormat='dd-MM-yyyy H:mm'
// showTimeSelect
timeFormat='HH:mm'
placeholder='todo UpdatedAt...'
label='UpdatedAt'
/>
<Link className='btn btn-light mr-2' to='/todos'>
Cancel
</Link>
<button className='btn btn-primary mr-2' type='submit'>
{this.isUpdating ? 'Updating' : 'Create'}
</button>
</form>
);
}
}
I expect the output e.g when I complete todo form and click on submit button is "2020-09-18N14:00:30", reflected datetime value on database, but real output are empty createdAt and updatedAt datetime values.
What is wrong?
I'm calling my api from NewTodo.jsx
state = {
redirect: false
};
componentDidMount() {
this.props.newTodo();
}
submit = todo => {
return this.props
.saveTodo(todo)
.then(response => this.setState({ redirect: true }))
.catch(err => {
throw new SubmissionError(this.props.errors);
});
};
回答1:
Step 1 : convert your date to string in your schema
schema
createdAt: String,
updatedAt: String
you can direclly store your data as you passed in your post request, if you want to convert it to required format then
Step 2 : use moment if you want to format your date in required format then install moment
npm install moment
const { name, description, isDone, createdAt, updatedAt} = req.body;
const todo = new Todo({
name,
description,
isDone,
createdAt : moment(createdAt).format('DD-MM-YYYY HH.mm') , //change it as per your requirement
updatedAt : moment(updatedAt).format('DD-MM-YYYY HH.mm') ,
});
来源:https://stackoverflow.com/questions/59817000/cant-save-createdat-and-updatedat-as-datetime-values-nor-backend-neither-fronte