Some code only works while chrome debugger is active

旧巷老猫 提交于 2020-07-09 07:36:15

问题


I'm working on a react-native application and I'm trying to format a date using moment.

Dates look like "02-16-2016 09:04:23"

function formatTime(date){

  var formattedDate = moment(date).format('MM:ss A');

  return formattedDate;
}

Works fine if the chrome debugger is active. But if I disable it, all I get is "invalid date"

Same thing with a decoding function I'm using

var that = this;
MessagesService.getMessageBody(selectedMessage)
    .then(function(messageBody){

        var decodedData = window.atob(messageBody.messages);

        that.setState({
          messageBody: decodedData
        })
    })
    .catch(function(err){
        console.log(err);
    })

Displaying the decoded data with

<Text> Body: {this.state.messageBody} </Text>

and displaying the date with

<View style = {[MessageStyles.senderItem, MessageStyles.date]}>
       <Text>
          {this.formatTime(message.createDateTime)}
       </Text>
</View>

Maybe this is a bad way to do this in react native? Still learning so I could be doing some bad practice.


回答1:


I learned recently that when using the chrome debugger, react native uses a different JS engine. The chrome JS engine is used during debugging, but JavaScriptCore is used otherwise. Per this article

http://ruoyusun.com/2015/11/01/things-i-wish-i-were-told-about-react-native.html

But for the actual issue with the dates, the JavaScriptCore engine doesn't seem to like parsing dates with a -. I had to use a regex expression to replace the - with a / and then all my date manipulation worked fine.

02-16-2016 09:04:23 is considered invalid

02/16/2016 09:04:23 is considered valid



来源:https://stackoverflow.com/questions/35660278/some-code-only-works-while-chrome-debugger-is-active

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!