我从一个javascript函数调用API,它正确地返回了一个JSON响应。但是,在单步执行JSON响应时,当我使用moment().isSameOrBefore
命中该行时,我在Function.createFromInputFallback (moment.js行:320)处收到了一个弃用警告和一个错误。我对javascript是个新手,尤其是Node的moment包。
基本上,我想确定在执行此函数时,哪个预测潮汐最接近当前时间。这是使用moment().isSameOrBefore
参数的正确方式吗?或者,我是否应该修改代码,以不同的方式执行此操作?
这是:
{ "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"} ]}
下面是我的函数:
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();
https://stackoverflow.com/questions/50768285
复制相似问题