<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Abdul Rauf</title>
    <description>The latest articles on DEV Community by Abdul Rauf (@abdullrauf).</description>
    <link>https://dev.to/abdullrauf</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1218022%2F31a44f99-3329-4144-b550-b1a47df73ed4.png</url>
      <title>DEV Community: Abdul Rauf</title>
      <link>https://dev.to/abdullrauf</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/abdullrauf"/>
    <language>en</language>
    <item>
      <title>Convert firestore admin timestamp to date in javascript/nodejs/nextjs</title>
      <dc:creator>Abdul Rauf</dc:creator>
      <pubDate>Fri, 24 Nov 2023 13:29:01 +0000</pubDate>
      <link>https://dev.to/abdullrauf/convert-firestore-admin-timestamp-to-date-cj6</link>
      <guid>https://dev.to/abdullrauf/convert-firestore-admin-timestamp-to-date-cj6</guid>
      <description>&lt;p&gt;Recently I was working on a Nextjs project where I was using @google-cloud/firestore package to write and read data from firestore.&lt;/p&gt;

&lt;p&gt;I had createdAt and updatedAt timestamps inside documents which I wanted to convert to data object on the frontend.&lt;/p&gt;

&lt;p&gt;Since timestapms created using server side firestore package (@google-cloud/firestore or firebase-admin/firestore) timestamps containes "_" prefix with seconds and nanoseconds keys&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{ _seconds: 1700832176, _nanoseconds: 968000000 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and if we try to convert this timestamp to date on frontend it gives error.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;TypeError: user.updatedAt.toDate is not a function
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To fix this error you have to convert this timestamp to date object on the server. for example in Nextjs api route.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { NextResponse } from "next/server";
import { db } from "./database";


export async function GET(req: Request) {

  try {
    const collectionRef = db
    .collection("users")

  const snapshot = await collectionRef.get();
  const users = snapshot.docs.map((doc) =&amp;gt; {
    const data = doc.data();
    return {
      id: doc.id,
      ...data,
      updatedAt: data.updatedAt.toDate(),
      appointmentAt: data.appointmentAt.toDate(),
    };
  });
  return NextResponse.json(appointments);

  } catch (error) {
    return NextResponse.json(error, {status: // status you want send })
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>javascript</category>
      <category>firebaseadmin</category>
      <category>node</category>
      <category>nextjs</category>
    </item>
  </channel>
</rss>
