首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用云函数时,firestore中的时间戳将转换为地图

使用云函数时,firestore中的时间戳将转换为地图
EN

Stack Overflow用户
提问于 2019-05-22 03:24:57
回答 7查看 3.6K关注 0票数 9

因此,我在云firestore中有一个Timestamp。我正在使用云函数从firestore到flutter检索数据。但是JSON格式化时间戳到映射,由于这我不能使用它作为时间戳。如何将其再次转换为时间戳?

这就是我将时间戳放到firestore的方法。

var reference = Firestore.instance.collection('posts');
      reference.add({
        'postTitle': this.title,
        'timestamp': DateTime.now(),
        'likes': {},
        'ownerId': userId,
      })

要检索数据,代码如下:

 factory Post.fromJSON(Map data){
    return Post(
      timestamp: data['timestamp'],
    );
  }
List<Post> _generateFeed(List<Map<String, dynamic>> feedData) {
    List<Post> listOfPosts = [];

    for (var postData in feedData) {
      listOfPosts.add(Post.fromJSON(postData));
    }

    return listOfPosts;
  }

但是这会返回一个错误。

I/flutter (17271): The following assertion was thrown building FutureBuilder<DocumentSnapshot>(dirty, state:
I/flutter (17271): _FutureBuilderState<DocumentSnapshot>#1536b):
I/flutter (17271): type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Timestamp'

这是我的云函数。

getFeed.ts

import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';

export const getFeedModule = function(req, res){
    const uid = String(req.query.uid);

    async function compileFeedPost(){
        const following = await getFollowing(uid, res)as any;

        let listOfPosts = await getAllPosts(following, res);

        listOfPosts = [].concat.apply([], listOfPosts);

        res.send(listOfPosts);
    }

    compileFeedPost().then().catch();
}

async function getAllPosts(following, res) {
    let listOfPosts = [];

    for (let user in following){
        listOfPosts.push( await getUserPosts(following[user], res));
    }
    return listOfPosts;
}

function getUserPosts(userId, res){
    const posts = admin.firestore().collection("posts").where("ownerId", "==", userId).orderBy("timestamp")

    return posts.get()
    .then(function(querySnapshot){
        let listOfPosts = [];

        querySnapshot.forEach(function(doc){
            listOfPosts.push(doc.data());
        });

        return listOfPosts;
    })
}

function getFollowing(uid, res){
    const doc = admin.firestore().doc(`user/${uid}`)
    return doc.get().then(snapshot => {
        const followings = snapshot.data().followings;

        let following_list = [];

        for (const following in followings){
            if (followings[following] === true){
                following_list.push(following);
            }
        }
        return following_list;
    }).catch(error => {
        res.status(500).send(error)
    })
}

云函数index.ts

import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
import { getFeedModule } from "./getFeed"
admin.initializeApp();

export const getFeed = functions.https.onRequest((req, res) => {
    getFeedModule(req, res);
})

由此调用

_getFeed() async {
    print("Starting getFeed");
    FirebaseUser user = await FirebaseAuth.instance.currentUser();

    SharedPreferences prefs = await SharedPreferences.getInstance();

    String userId = user.uid;
    var url =
        'https://us-central1-jaluk-quiz.cloudfunctions.net/getFeed?uid=' + userId;
    var httpClient = HttpClient();

    List<QuizViewer>listOfPosts;
    String result;
    try {
      var request = await httpClient.getUrl(Uri.parse(url));
      var response = await request.close(); 
      if (response.statusCode == HttpStatus.ok) {
        String json = await response.transform(utf8.decoder).join();
        prefs.setString("feed", json);
        List<Map<String, dynamic>> data =
            jsonDecode(json).cast<Map<String, dynamic>>();
        listOfPosts = _generateFeed(data);
        result = "Success in http request for feed";
      } else {
        result =
            'Error getting a feed: Http status ${response.statusCode} | userId $userId';
      }
    } catch (exception) {
      result = 'Failed invoking the getFeed function. Exception: $exception';
    }
    print(result);

    setState(() {
      feedData = listOfPosts;
    });
  }
EN

回答 7

Stack Overflow用户

回答已采纳

发布于 2019-05-22 06:16:47

如果您正在处理的时间戳已被序列化为具有秒和纳秒组件的对象,则可以使用这些组件通过new Timestamp(seconds, nanoseconds)创建新的Timestamp对象。

票数 5
EN

Stack Overflow用户

发布于 2019-09-10 14:38:53

事实上,在使用云函数时,时间戳会以普通地图的形式返回。但是如果你使用Firebase SDK,它会返回Timestamp对象。我使用以下函数来处理这两种情况:

/// https://stackoverflow.com/a/57865272/1321917
DateTime dateTimeFromTimestamp(dynamic val) {
  Timestamp timestamp;
  if (val is Timestamp) {
    timestamp = val;
  } else if (val is Map) {
    timestamp = Timestamp(val['_seconds'], val['_nanoseconds']);
  }
  if (timestamp != null) {
    return timestamp.toDate();
  } else {
    print('Unable to parse Timestamp from $val');
    return null;
  }
}

json_annotation库完美配合:

  @JsonKey(
      fromJson: dateTimeFromTimestamp,
      toJson: dateTimeToTimestamp,
      nullable: true)
  final DateTime subscriptionExpiryDate;
票数 6
EN

Stack Overflow用户

发布于 2021-03-26 23:02:29

您可以使用converted接收DateTime,如下所示:

class TimestampConverter implements JsonConverter<DateTime, dynamic> {
  const TimestampConverter();

  @override
  DateTime fromJson(dynamic data) {
    Timestamp timestamp;
    if (data is Timestamp) {
      timestamp = data;
    } else if (data is Map) {
      timestamp = Timestamp(data['_seconds'], data['_nanoseconds']);
    }
    return timestamp?.toDate();
  }

  @override
  Map<String, dynamic> toJson(DateTime dateTime) {
    final timestamp = Timestamp.fromDate(dateTime);
    return {
      '_seconds': timestamp.seconds,
      '_nanoseconds': timestamp.nanoseconds,
    };
  }
}

然后像这样标记你的模型字段:

@TimestampConverter() DateTime createdAt
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56245156

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档