【发布时间】:2019-12-19 03:18:34
【问题描述】:
第 1 步:我正在从天气提供商那里阅读,他们提供 Unix、UTC 的日出时间 - 类似于“1565616151”
第 2 步:我是使用“var sunrise = new Date(openWeatherData.sys.sunrise*1000)”将其转换为 Javascript 日期
第 3 步:我需要将其作为时间戳存储在我的 Firestore 文档中。但我无法从第 2 步转换日期
我尝试过使用:
admin.firestore.Timestamp.fromDate(sunrise)
这给了我控制台上的以下错误
TypeError: 无法读取未定义的属性“getTime”
在 Function.fromDate (/srv/node_modules/@google-cloud/firestore/build/src/timestamp.js:64:42)
相关代码:
request(url, (error, response, body) => {
if (response.statusCode === 200) {
console.log('Successfully retrieved from openweather');
var openWeatherData = JSON.parse(body);
console.log(`Output from OpenWeather for Sunrise:${openWeatherData.sys.sunrise}` );
var sunrise = new Date(openWeatherData.sys.sunrise*1000);
console.log(`Sunrise after conversion to JS Date:${sunrise}` );
}
else {
var openWeatherDataError = JSON.parse(body);
console.log('Issue fetching from open weathermap. Status code: ' + openWeatherDataError.cod
+ 'Message: ' + openWeatherDataError.message);
}
});
admin.firestore().collection('myCollectionPath')
.doc('myDoc')
.set(
{
'status': 'initiated',
'initiationTime': FieldValue.serverTimestamp(),
'sunrise': admin.firestore.Timestamp.fromDate(sunrise),
}
)
.then(doc => {
return res.status(200).send('Stored Successfully');
})
.catch(err => {
console.error(err);
return res.status(404).send({ error: 'unable to store', err });
});
}
);
预期结果:文档将成功保存到 Firestore
【问题讨论】:
标签: firebase google-cloud-firestore timestamp google-cloud-functions