`serverTimestamp()` is not being updated correctly while other data is written properly when the user is not logged in

When the user wants to connect with an expert, they submit data in the below format.

SearchTalent.js component

const handleSubmit = async () => {
  const loginState = localStorage.getItem("authenticated");
  let uploads = await uploadSelectedFiles();
  const saveData = {
    // taskName: taskName,
    description: description,
    loomLink: loomLink,
    uploadFiles: uploads,
    expertCategories: expertCategories,
    post_userid: currentUserUid,
    posterRole: myRole,
    timestamp: serverTimestamp(),
  };
  if (loginState == "true") {
    setLoader(true);
    let newJobId = await writeJobPostToDb(saveData);
    console.log(newJobId, "this is a hilarious jobid");
    // Get the updated document from Firestore to access the actual timestamp
    sendNewJobNotify(newJobId, skillSet, expertCategories);
    navigate(`/Portfolio/id=${currentUserUid}/order-history`);
    localStorage.removeItem("jobPostData");
  } else {
    // localStorage.setItem("taskName", JSON.stringify(taskName));
    localStorage.setItem(
      "expertCategories",
      JSON.stringify(expertCategories)
    );
    //navigate(`/Portfolio/id=${currentUserUid}/order-history`);
    localStorage.setItem("jobPostData", JSON.stringify(saveData));
    navigate("./register");
  }
};

common.js utils

export const writeJobPostToDb = async (data) => {
  const docRef = await addDoc(collection(db, "jobpost"), data);
  return docRef.id;
};

Customers post their requirements to find out the experts and those data are stored on the Firestore. It works as normal when the user logged in, but it doesn’t log the timestamps correctly when the user is not logged in.

In normal, it should be like this.

timestamp
September 2, 2024 at 10:19:56 AM UTC-7

But seeing this on the jobpost data that created before the user Signup to the platform.

timestamp     (map)
_methodName: "serverTimestamp" (string)

The whole jobpost collection field looks like this.

description: "" (string)

expertCategories: (array)
0 "Nuke" (string)

loomLink: "" (string)

post_userid: "v7mCJl07XLO3kQL6AWC7" (string)

posterRole: ""(string)

timestamp:  (map)
    _methodName: "serverTimestamp" (string)

uploadFiles

Other fields are fine, but only the timestamp not working.
I’ve checked the Firestore rule setup, and asyc/await till the DB update and I was able to reproduce this by triggering the handleSubmit without Login/Signup to the platform.

First time visiters of the website can also create the jobpost by triggering handleSubmit, and they will be redirecting to the register page first. Temporary jobpost data created by the user before Signup, first stored on the localStorage.

I changed above code to this and solve the issue temporarily.

const handleSubmit = async () => {
  const loginState = localStorage.getItem("authenticated");

  if (loginState == "true") {
    let uploads = await uploadSelectedFiles();
    const saveData = {
      description: description,
      loomLink: loomLink,
      uploadFiles: uploads,
      expertCategories: expertCategories,
      post_userid: currentUserUid,
      posterRole: myRole,
      timestamp: serverTimestamp(),
    };

    setLoader(true);
    let newJobId = await writeJobPostToDb(saveData);
    sendNewJobNotify(newJobId, skillSet, expertCategories);

    navigate(`/Portfolio/id=${currentUserUid}/order-history`);
    localStorage.removeItem("jobPostData");
  } else {
    localStorage.setItem(
      "expertCategories",
      JSON.stringify(expertCategories)
    );
    navigate("./register");
  }
};

App.js

return (
  <BrowserRouter>
    <Routes>
      <Route path="/" element={<Layout onlogin={isLoggedIn} />}>
        <Route path="/login" element={<Login />} />
        <Route index element={<Home />} />
        <Route element={<PrivateRoute />}>
           <Route path="/order-instant" element={<SearchTalent />} />

Home.js component

function Home() {
  return (
    <div className="flex w-full p-0 sm:p-6 px-auto">
        <SearchTalent />

I am using the Firebase SDK version ^9.23.0

Has anyone else encountered this issue? I really hop for some assistance on this!

7

You can instead use firebase.firestore.TimeStamp.now().toDate() to get a similar result.

https://firebase.google.com/docs/reference/js/v8/firebase.firestore.Timestamp

end result will look like this 2024-09-25T06:50:47.426Z

3

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật