I got a Report model in MongoDB/Mongoose, most of them are String
types, one is Boolean
and two Date
types with default to Date().now
date_posted: {
type: Date,
default: Date.now
},
date_lts: {
type: Date,
default: Date.now
},
approved: {
type: Boolean,
required: true
}
Now, from React Native, I'm doing a POST to my NodeJS app, where I use momentJS and new Date() for those fields.
date_posted: new Date(),
date_lts: moment(this.state.date).format(),
This is what the Server is receiving:
date_posted: '2017-05-12T00:43:09.461Z',
date_lts: '2017-05-11T00:00:00-03:00',
I'm thinking that since those are Strings, it might have some problems while validating those values.
So how should I build the POST HTTP request from React Native/Javascript for dates?
This would be the entire fetch()
from my React Native app.
var url = Constants.API_URL + '/reports/save';
fetch(url, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
status: 'active',
image_url: this.state.imageUploadData.image_url,
image_name: this.state.imageUploadData.image_name,
date_posted: new Date(),
date_lts: moment(this.state.date).format(),
name: this.state.fullName,
country: this.props.countryName,
approved: false
})
})
Then in my app.js I got the following:
app.post('/api/reports/save', function(req, res) {
var report = req.body;
Report.addReport(report, function(err, report) {
if(err) {
throw err;
}
res.json(report);
});
})
Then in my report model, i got this addReport() method which is like this:
module.exports.addReport = function(report, callback) {
Report.create(report, callback);
}
In app.js server is throwing this error:
events.js:160
throw er; // Unhandled 'error' event
^
ValidationError: Report validation failed
at MongooseError.ValidationError (/Users/marian/Documents/dev/exampleapp/node_modules/mongoose/lib/error/validation.js:23:11)
via msqar
No comments:
Post a Comment