Reputation: 513
I'm making an API call from a javascript function which is returning a JSON response correctly. But when stepping through the JSON response I'm getting a deprecation warning as well as an error at Function.createFromInputFallback (moment.js line:320) when I hit the line with moment().isSameOrBefore
. I'm fairly new to javascript and especially the moment package for Node.
Basically, I would like to establish which predicted tide is the closest to the current time when this function is executed. Is this the correct way to use the moment().isSameOrBefore
paramter and/or should I modify the code to go about this differently?
Here's the JSON:
{ "predictions" : [ {"t":"2018-06-08 03:06", "v":"3.795", "type":"H"},{"t":"2018-06-08 09:25", "v":"0.443", "type":"L"},{"t":"2018-06-08 15:51", "v":"3.884", "type":"H"},{"t":"2018-06-08 22:01", "v":"0.778", "type":"L"} ]}
Here's my function:
const getTide = require('./modules/getTide.js');
var moment = require('moment');
async function testMod() {
await getTide.getQuote().then(function(value){
const parsedData = JSON.parse(value);
let text = " ";
// This loop steps through the JSON response row by row to test the data
var i;
for (i = 0; i < parsedData.predictions.length; i++) {
text += 'Record #' + i + ' = ' + parsedData.predictions[i].t + " " + parsedData.predictions[i].v + " " + parsedData.predictions[i].type + " - ";
let curDateTime = moment().format('LLL');
let theDate = moment(parsedData.predictions[i].t).format('LLL');
let fromNow = moment(parsedData.predictions[i].t).fromNow();
if (parsedData.predictions[i].type === "H") {
console.log('High tide for ' + parsedData.predictions[i].t + ', at ' + parsedData.predictions[i].v + ' vertical Feet. ');
if (moment(theDate).isSameOrBefore(curDateTime)) {
console.log('It is currently ' + curDateTime + ' and that high tide was ' + fromNow);
} else {
console.log('It is currently ' + curDateTime + ' and that high tide is ' + fromNow + ' from now!');
}
} else {
console.log('Low tide for ' + parsedData.predictions[i].t + ', at ' + parsedData.predictions[i].v + ' vertical Feet. ');
if (moment(theDate).isSameOrBefore(curDateTime)) {
console.log('It is currently ' + curDateTime + ' and that low tide was ' + fromNow);
} else {
console.log('It is currently ' + curDateTime + ' and that low tide is ' + fromNow + ' from now!');
}
}
}
}, function(error){
console.log("problem");
});
}
testMod();
Upvotes: 0
Views: 88
Reputation: 2649
I think part of the issue is you're using formatted strings to create moment instances instead of just using the moment instance itself. So instead of doing this:
let curDateTime = moment().format('LLL');
let theDate = moment(parsedData.predictions[i].t).format('LLL');
// ...
moment(theDate).isSameOrBefore(curDateTime);
Try:
let curDateTime = moment();
let theDate = moment(parsedData.predictions[i].t);
// ...
theDate.isSameOrBefore(curDateTime); // because theDate is now a moment instance, you can call this function on it
When working with moment, it's always a good idea to keep your datetimes stored as moment instances until you need to display them to the user, then you can go ahead and do:
theDate.format('LLL');
I think the warning you are getting is because you are trying to create a moment instance with a string in the 'LLL' format which moment is complaining about ("Deprecation warning: value provided is not in a recognized RFC2822 or ISO format." is the warning I am seeing). If you wanted to parse these formats you have to specify the format as well:
moment('September 4, 1986 8:30 PM', 'LLL').format('YYYY-MM-DD H:mm'); // won't complain
Upvotes: 1