我遇到一个奇怪的运行时错误,似乎与moment()
函数有关,如下所示:
Deprecation warning: value provided is not in a recognized RFC2822 or ISO format. moment construction falls back to js Date(), which is not reliable across all browsers and versions. Non RFC2822/ISO date formats are discouraged and will be removed in an upcoming major release. Please refer to http://momentjs.com/guides/#/warnings/js-date/ for more info.
Arguments:
[0] _isAMomentObject: true, _isUTC: false, _useUTC: false, _l: undefined, _i: function now() { [native code] }, _f: undefined, _strict: undefined, _locale: [object Object]
Error
at Function.createFromInputFallback (C:\Users\John\dev\blah\node_modules\moment\moment.js:320:98)
at configFromInput (C:\Users\John\dev\blah\node_modules\moment\moment.js:2623:19)
at prepareConfig (C:\Users\John\dev\blah\node_modules\moment\moment.js:2594:13)
at createFromConfig (C:\Users\John\dev\blah\node_modules\moment\moment.js:2561:44)
at createLocalOrUTC (C:\Users\John\dev\blah\node_modules\moment\moment.js:2648:16)
at createLocal (C:\Users\John\dev\blah\node_modules\moment\moment.js:2652:16)
at hooks (C:\Users\John\dev\blah\node_modules\moment\moment.js:12:29)
at auth (C:\Users\John\dev\blah\controllers\users_controller.js:77:32)
at C:\Users\John\dev\blah\node_modules\async\dist\async.js:3880:24
at replenish (C:\Users\John\dev\blah\node_modules\async\dist\async.js:1011:17)
at C:\Users\John\dev\blah\node_modules\async\dist\async.js:1016:9
at eachOfLimit (C:\Users\John\dev\blah\node_modules\async\dist\async.js:1041:24)
at C:\Users\John\dev\blah\node_modules\async\dist\async.js:1046:16
at eachOf (C:\Users\John\dev\blah\node_modules\async\dist\async.js:1117:5)
at _parallel (C:\Users\John\dev\blah\node_modules\async\dist\async.js:3879:5)
at Object.parallelLimit [as parallel] (C:\Users\John\dev\blah\node_modules\async\dist\async.js:3962:5)
(node:1772) [DEP0079] DeprecationWarning: Custom inspection function on Objects via .inspect() is deprecated
我认为该错误来自users_controller.js
中的以下代码:
var moment = require('moment');
exports.user_create_get = function(req, res, next) {
async.parallel({
personalData: function(callback){
var personalDataObj = new PersonalData({
dateOfBirth: moment('1900-01-01'),
sex: '',
maritalStatus: ''
})
},
id: function(callback){
var idObj = new Id({
dateProvided: moment('1900-01-01'),
idDoc: ''
})
},
photo: function(callback){
var photoObj = new Photo({
dateProvided: moment('1900-01-01'),
photoDoc: ''
})
},
}, function(err, result){
if (err) {return next(err);}
var user = new User({
personalData: result.personalData,
idData: [result.id],
photoData: [result.photo],
})
console.log("Sending JSON . . . ");
res.json(JSON.parse(user));
})
};
我已经上网了,我认为我正确地使用了moment()。我尝试将日期格式设置为'01 / 01/1900','1900-01-01'-查看错误说明,我什至不确定错误与moment()
有关。另外,我不了解弃用警告,如果只是警告,为什么代码会崩溃?
非常感谢,如果有什么想法导致此错误!
答案 0 :(得分:0)
供应格式修复了该错误。也就是说,使用moment("1900-01-01","YYYY-MM-DD").format("YYYY MM DD")
可以消除错误。