【问题标题】:convert Unix time to firstore timestamp in a cloud function将 Unix 时间转换为云函数中的 firstore 时间戳
【发布时间】: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


    【解决方案1】:

    首先是将写入sunrise 到Firestore 的代码移入设置sunrise 值的块中。

    所以:

    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}` );
    
          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 });
          });
        }
        else {
          var openWeatherDataError = JSON.parse(body);
          console.log('Issue fetching from open weathermap. Status code: ' + openWeatherDataError.cod
            + 'Message: ' + openWeatherDataError.message);
        }
      });
    });
    

    如果这仍然给您带来问题,请检查您获得的日志输出。

    【讨论】:

    • 非常感谢。觉得很傻。并表明我是一个生疏的程序员!
    猜你喜欢
    • 1970-01-01
    • 2023-03-18
    • 2013-04-07
    • 2020-10-19
    • 1970-01-01
    • 1970-01-01
    • 2020-07-29
    • 1970-01-01
    • 2010-10-25
    相关资源
    最近更新 更多