【发布时间】:2015-12-22 22:40:32
【问题描述】:
我在 Angular 代码和用 nodejs 编写的实用程序应用程序中都使用了 momentjs。 angular中有一个实用函数如下:
function _fromMomentPeriod(period) {
return {
start: period.start ? period.start.format() : undefined, // keep the tz info. Do not convert to utc
end: period.end ? period.end.format() : undefined, // keep the tz info. Do not convert to utc
};
}
不用说,period.start 和 period.end 是 moment 对象。这在浏览器中运行良好,但在节点中,我得到“格式不是函数”。当我检查 period.start 对象时,它是一个时刻对象,但我没有看到它上面的格式函数。它看起来像这样:
{
"_isAMomentObject": true,
"_isUTC": false,
"_locale": {
"_ordinalParse": {},
"_abbr": "en",
"_ordinalParseLenient": {}
},
"_d": "2015-12-23T14:48:37.383Z"
}
我是节点新手,所以我认为我遗漏了一些明显的东西。这是什么?
【问题讨论】:
-
你看不到格式函数的原因是因为这些函数是你的时刻对象原型的一部分。
-
试试
console.log(Object.keys(period.start.__proto__)) -
@see-sharper 你如何初始化变量
period? -
@Quy:我想知道 - 我在 Visual Studio Code 中进行检查,它不如 Chrome 调试器有用。但这并不能解释为什么我得到“格式不是函数”。
-
@stdob:它被转换为另一个函数中的时刻,如下所示:period.start = moment(period.start)。最初它是一个字符串。
标签: javascript node.js momentjs