admin管理员组文章数量:1435740
I have a problem with momentjs and I have no idea why. I wrote this code a couple of days ago and it worked fine then, but today I got this error: Unhandled Rejection (TypeError): Cannot read property 'moment' of undefined
.
import React, { Component } from 'react'
import moment from 'moment'
import Datetime from 'react-datetime'
import './react-datetime.css'
let parseDate = str => {
if (moment(str).isValid()) {
return moment(str).toISOString()
} else {
return moment(str).toISOString()
}
}
export default class DateInput extends Component {
render() {
const yesterday = Datetime.moment().subtract(1, 'day') //Error occur on this line.
const valid = current => {
return current.isAfter(yesterday)
}
const Datetime = (
<div>
<Datetime
isValidDate={valid}
timeFormat="HHmm"
dateFormat="YYYY-MM-DD"
onChange={e => {
this.props.onChange(parseDate(e._d))
}}
/>
</div>
)
if (this.props.value) {
return { Datetime }
} else {
return <p>Inget datum angivet</p>
}
}
}
I bet the problem is really basic, but I have no idea why this is happening.
Thanks for reading and I hope we can solve it!
I have a problem with momentjs and I have no idea why. I wrote this code a couple of days ago and it worked fine then, but today I got this error: Unhandled Rejection (TypeError): Cannot read property 'moment' of undefined
.
import React, { Component } from 'react'
import moment from 'moment'
import Datetime from 'react-datetime'
import './react-datetime.css'
let parseDate = str => {
if (moment(str).isValid()) {
return moment(str).toISOString()
} else {
return moment(str).toISOString()
}
}
export default class DateInput extends Component {
render() {
const yesterday = Datetime.moment().subtract(1, 'day') //Error occur on this line.
const valid = current => {
return current.isAfter(yesterday)
}
const Datetime = (
<div>
<Datetime
isValidDate={valid}
timeFormat="HHmm"
dateFormat="YYYY-MM-DD"
onChange={e => {
this.props.onChange(parseDate(e._d))
}}
/>
</div>
)
if (this.props.value) {
return { Datetime }
} else {
return <p>Inget datum angivet</p>
}
}
}
I bet the problem is really basic, but I have no idea why this is happening.
Thanks for reading and I hope we can solve it!
Share Improve this question edited Jul 16, 2017 at 21:33 Rob 15.2k30 gold badges48 silver badges73 bronze badges asked Jul 16, 2017 at 21:26 Martin NordströmMartin Nordström 6,02511 gold badges33 silver badges66 bronze badges 2-
1
Why do you over-write the
Datetime
object? – XCS Commented Jul 16, 2017 at 21:30 - What do you mean @Cristy? – Martin Nordström Commented Jul 16, 2017 at 21:36
2 Answers
Reset to default 1import Datetime from 'react-datetime'
makes Datetime
available for use in the scope of your script. However you set it again inside render
const Datetime = (
<div>
<Datetime
isValidDate={valid}
timeFormat="HHmm"
dateFormat="YYYY-MM-DD"
onChange={e => {
this.props.onChange(parseDate(e._d))
}}
/>
</div>
)
In javascript, function's scope takes precedence over the global/script scope (where your function is declared). And since you used Datetime
in a statement before the declaration you're referencing to an undefined
variable/constant
P.S. In ECMAScript 2015, let (const) will not hoist the variable to the top of the block. However, referencing the variable in the block before the variable declaration results in a ReferenceError. The variable is in a "temporal dead zone" from the start of the block until the declaration is processed.
Moment takes the argument inside itself, its a global function.
I suppose Datetime has no such function like 'moment'.
maybe
moment(Datetime)
?
const yesterday = moment(Datetime).subtract(1, 'day')
and i don't know what is Datetime in your code... take a look at Moment.js: Parsing
本文标签: javascriptCannot read property 39moment39 of undefinedStack Overflow
版权声明:本文标题:javascript - Cannot read property 'moment' of undefined - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745638541a2667710.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论