having issues converting mongodb data to json in dart server side

Viewed 47

I am trying to get a list of friends from the database and send it to the client using shelf_router. I am able to get the data but it is not parsed correctly, so the client does not recognize it as a json object. When I print friendsList to console it is formatted like this:

[{username: dave@one.com, sent: 2021-05-17 22:16:37.752Z}, {username: josh@one.com, sent: 2021-05-17 22:18:39.183Z}, {username: adam@one.com, sent: 2021-05-17 22:29:18.051Z}, {username: mike@one.com, sent: 2021-05-17 22:32:35.713Z}, {username: rob@one.com, sent: 2021-05-17 22:34:29.328Z}, {username: larry@one.com, sent: 2021-05-17 23:20:32.737Z}]

Here is the code I am currently using:

 router.get('/', (Request req) async {
  final authDetails = req.context['authDetails'] as JWT;
  final user = await store.findOne(
      where.eq('_id', ObjectId.fromHexString(authDetails.subject)));

  var friendsList = user['friends'];

  print(friendsList);

  return Response.ok(json.encode(friendsList.toString(), headers: {
    'Content-Type': 'application/json',
  });
});

Right now, they are showing up like map objects but I gotta have them all in one json object so the client side can understand the data. Thanks in advance

0 Answers
Related