#Submitting Data to FireStore isn't working (Please Help)

171 messages ยท Page 1 of 1 (latest)

grizzled osprey
#

Hey so i'm creating my first ever firebase (for hte backend) app, to learn and try out what i know.

And my project, simply put is a Job Board. A service much needed for my local area due to the absence of good job boards.

Anyhow, I'm at a stage of "completing user data" (i'll mention routes below), and there's two children components, one for the "employer" and one for the "job seeker". The employer data was being submitted with no problems whatsoever.
However, when the user is trying to complete their data, I seemingly get no errors in the console, but the data is simply not being uploaded. it's really confusing and I don't know what to do.

Here's my repo below, and I really appreciate help, since I don't know what i'm doing:
https://github.com/mhdalihoria/job-hub

Route:

  • The problem is happening in /profile-edit and its children components SeekerForm.jsx and EmployerForm.jsx.
  • To reach /profile-edit you need to create an account, and after logging in, go to your user icon and go to profile

Notes:

  • I don't know how the debugging would work since it's a backend problem, but I guess one can change hte firebase key and work on their own backend.

Thanks a lot in advance, this is a project i'm beyond excited to see it come to light so any help is greatly appreciated.

GitHub

Contribute to mhdalihoria/job-hub development by creating an account on GitHub.

jagged vigil
#

@grizzled osprey When submitting the data from the SeekerForm, what http response code are you getting in return? You can see this by looking at the browser dev tools "Network" tab.

grizzled osprey
#

as you can see, all of hte response codes are 200

jagged vigil
#

Hmm odd. Have you checked the firebase usage logs to see if there's anything weird happening there?

grizzled osprey
jagged vigil
#

Hmm, not sure what to suggest. Been awhile since I used Firebase so I can't test out your code myself. Maybe someone else can see what's wrong @grizzled osprey

grizzled osprey
#

hrm, i see. Thanks for trying anyway. Your efforts are appreciated

smoky thorn
#

@grizzled osprey

#

Are you using a paid plan or an unpaid plan?

grizzled osprey
smoky thorn
#

ok

#

which type of data you are sending in the API

#

@grizzled osprey

grizzled osprey
#

basic types, objects, text. that sort of thing

#

you can see it in my repo above

smoky thorn
#

json + file right

grizzled osprey
#

I don't believe i'm sending JSON, i'm sending a normal set of data, as for the file, it's uploaded* to storage and I only have hte link sent to firestore

smoky thorn
#

Image is uploading?

grizzled osprey
#

it's successfully being uploaded to storage on each submit, yes

smoky thorn
#

just firestore not working

grizzled osprey
#

just firestore with the seeker form, as the employer form seems to be working fine

smoky thorn
#

const handleFinalSubmit = async () => {
// create a state or a function that we pass to EmployerForm and SeekerForm and pass the values to it for it to be submitted into firebase
// This makes sure the info submitting is getting handled in a central place
console.log("final submit");
try {
const userId = userData.uid
const timestamp = new Date().getTime();
// replace "formPreviewData.reseme.name" with the name of the file uploaded
const fileName = ${userId}/${timestamp}_${formPreviewData.reseme.name};
const storageRef = ref(storage, fileName);
console.log(storageRef);
// replace "formPreviewData.reseme" with the actual file uploaded
await uploadBytes(storageRef, formPreviewData.reseme).then((snapshot) => {
console.log("Uploaded a blob or file!", snapshot);
});

  const downloadURL = await getDownloadURL(storageRef);
  console.log("Download URL:", downloadURL);
} catch (err) {
  console.error(err);
}

};
In this, where are you sending the image URL to Firestore?
seeker form

#

@grizzled osprey

grizzled osprey
final linden
#

I think in your employerForm.jsx, you are calling handleCompleteUsrProfile which is in profile-edit/index.jsx which sets the doc. In the seekerForm you are adding something storage but not setting the rest of the doc data anywhere as you don't call handleCompleteUsrProfile again, or code it within the handleFinalSubmit of seekerForm.

#

Basically, I don't see setDoc or updateDoc anywhere in the seekerForm component, and you aren't calling handleCompleteUsrProfile which has that function

#

It looks like all of the data is being sent to storage, is that what you meant to do?

#

Well, just the resume is being uplodaded to storage

#

But skills, desired job title, etc. don't seem to be handled anywhere expect set in state

smoky thorn
#

@grizzled osprey bro Are you trying to send this URL to Firestore, if I'm not mistaken?

final linden
#

he's using firebase storage as well

#

so i think the intent is to upload a resume to firebase storage, and store the user data in a firestore doc. But there is no setDoc or updateDoc being used in the seekerForm

smoky thorn
#

I'm able to find the url, but I'm not able to find the next process of sending this url to Firestore.

final linden
#

exactly

smoky thorn
#

@grizzled osprey

final linden
#

the url is a dowload url for firebase storage, i bet he sees someting in his console there

smoky thorn
#

right

final linden
#

but to set the other data collected by the form which he has saved in state, he needs to set or update a firebase doc

#

He does this in the employerForm by calling handleCompleteUsrProfile which is a function passed from profile-edit/index.jsx , which is why that component works but seekerForm doesn't

smoky thorn
#

@grizzled osprey

grizzled osprey
#

Thank you guys a lot for the replies. Something has come up irl, so I will look into it and reply to each message after i'md one.

Your effort and time are much appreciated

grizzled osprey
grizzled osprey
# final linden Well, just the resume is being uplodaded to storage

Yes, Only the reseme and Profile pictures (will be handled later) are meant to be used in storage imo. as the rest of the information belongs with the rest of the user-related data in firestore (user name, email, etc).

So my logic was, get the link from th storage, and then add it to the object assigned to the user's data in firestore

grizzled osprey
final linden
grizzled osprey
#

oh my apologies, it seems I have comitted some changes, but forgot to push

#

they must be live now

final linden
#

It looks like it should be good now?

grizzled osprey
#

latest commit should be "Error is still pressisting"

#

yes, that's the thing, it SHOULD be good, but it isn't

#

i get no errors, no nothing, which is what confuses me about the entire thing

final linden
#

let me take another look

grizzled osprey
#

earlier today i had this error

User
FirebaseError: Function setDoc() called with invalid data. Unsupported field value: a custom M object (found in document users/PHC6fjwUNMamvqLNd4Sqay7l3Uk1)```

Which after consulting GPT, it was probably the result of a "complex" initialValues (FormikFIeldValues) data, to which I have made it less complex. you can see that thru the commits
#

"PHC6fjwUNMamvqLNd4Sqay7l3Uk1" this bein the id of the user i am testing things on

grizzled osprey
final linden
#

looks like profile-edit/index is missing from your repo now

#

which is where handleCompleteUsrProfile was

grizzled osprey
#

it's... missing?

#

the _app.jsx changes are me commenting out some logs

final linden
grizzled osprey
#

profile-edit/index is in the pages directory

final linden
#

whoops lol

grizzled osprey
#

all g lol

#

also another weird behavior I encountered that I didn't get to fix, is:
After submitting from "employer form", then refreshing, and selecting the seeker form, it seems when user is submitting data, that the employer data (or at least some of it) is being submitted, even though you're a user. Weird bug that doesn't make sense, but may guide to the right answer

final linden
#

It may be best to break the submission of the data of the user and employer to firebase into two separate functions as they will have different document fields collected. It's great to have one utility function to do it all, but honestly separating the two won't cause the app to load any slower and could help you trouble shoot this by being distinct with what is being passed to each function.

#

Right now you are using a function(handleCompleteUsrProfile) that will get a doc, see if it exists, then set a new doc in its place. For the sake of firebase reads, you could probably do without getting the doc altogether in that function because if you set something or even update (if i recall correctly) something that doesn't exist it will create the new doc for you in firestore

#

And, there's no else within the if statement for if the docSnap exists. What if, and I didn't look this far, if the user doc wasn't created at login, the function is getting a doc that doesn't exist, so the execution is then stopped because there is nothing thrown in an else statement

grizzled osprey
final linden
#

only auth will work just fine... its chicken and the egg. You have to allow an auth user to create the doc for the client side to see if it exists, unless you are using cloud functions and the admin SDK

grizzled osprey
#

I think clouds functions are outside the spark plan, so i don't got access to that as of now

#

You have to allow an auth user to create the doc for the client side to see if it exists
But this is concerning, you do have a point, a new user, who isn't authed, may not be able to create since write is auth only

final linden
#

you can do blaze plan for free... you just have to provide a credit card. I've been doing a lot of firebase on this project you can check out if you want. Uses cloud functions, etc. It's very early stages and if you don't want to create an account, you can use test@home.net, password is 123456

grizzled osprey
#

oh wait, the rule is for firestore, so if a new user is created, it's auth related activity so it doesn't count

final linden
#

90s-online.netlify.app

#

ive done tons of reads and writes in developing this and haven't paid a penny

grizzled osprey
#

thing is, due to me being in a restricted 3rd world country, CCs aren't really available. But i'm trying to get my hands on one, at least have access to it

final linden
#

gotcha

#

i don't have a lot of time right now, but I can check out your code a bit later. If you want, you can give me access to your firebase as an admin temporarily and i'll fork your code and look at it. I understand if you wouldn't want to risk that though. I may be able to work through it without the firebase console

grizzled osprey
#

I don't know how to do that quite yet. Maybe by the time you're back I'll be done implimenting the suggestion you mentioned earlier, to break the function down each tailored to its own component, and we will see what we can do from there. Thanks a ton for the help so far

final linden
#

No problem, sorry we don't have it figured out just yet but I'm guessing it has something to do with a doc not existing or the way info is passed into the function from the component. best of luck and I'll catch up with you later

grizzled osprey
#

it's most likely it

final linden
#

PS., if you check out that site it is intended for desktop/laptop - not quite mobile friendly yet as the experince was supposed to be 1990's internet

grizzled osprey
grizzled osprey
final linden
#

So i looked at this some more... I forked your repo and set up my own firebase project to mess with this. The first issue seems to be with your form validation in the seekerForm component. On the seeker page, I couldn't get the back/submit buttons to appear like I could for the employer. I tried lots of stuff and console logs weren't getting me anywhere with the button events, so started looking closer at how the form is handled. I don't have experience using Formik, so someone else may be able to help you out from here. By removing the validation schema I was able to get to the next step. I was using the same auth user as I did for employer and it didn't overwrite the role from employer to job seeker. It did upload the photo to my storage though. So there is more work to be done, but first take a look at the validation schema.

You can see in the attached photo that with the schema commented out, it now progresses to the next step of previewing the form data and rendering the back and submit button.

#

@grizzled osprey

#

Also you were right, the credential I gave you was wrong. It was test1@home.net, through test8@home.net. You have a nice look to your site by the way!

grizzled osprey
# final linden So i looked at this some more... I forked your repo and set up my own firebase ...

That's quite confusing, as it seems (and I know every developer dreads hearing this) but it's working fine on my device, even with the schema untouched.

Also, that's the thing, I CAN upload the file to stroage, but I can't seem to get the data of the user to upload to firestore. Like as of now (without commenting out the schema) I am able to go back and fourth from the actual form to the preview part, when "submit" is pressed, it's uploading to storage but not more

grizzled osprey
smoky thorn
#

@grizzled osprey problem resolved or not

grizzled osprey
smoky thorn
#

Please share any live links.

grizzled osprey
smoky thorn
#

const firebaseConfig = {
apiKey: "AIzaSyBl_zipPqmOU-QRikEjnNlLq7lPYUEpIo4",
authDomain: "job-hub-42069.firebaseapp.com",
projectId: "job-hub-42069",
storageBucket: "job-hub-42069.appspot.com",
messagingSenderId: "895515432687",
appId: "1:895515432687:web:1dcfa7e04c0e8664baad1a",
};

change to .env

grizzled osprey
#

you mean change hte variables and use .env to conceal them? I tried, but i was getting some errors so I decided postponing that till everything is done

smoky thorn
#

ok

#

apiKey: process.env.NEXT_PUBLIC_API_KEY,
authDomain: process.env.NEXT_PUBLIC_AUTH_DOMAIN,
projectId: process.env.NEXT_PUBLIC_PROJECT_ID,
storageBucket: process.env.NEXT_PUBLIC_STORAGE_BUCKET,
appId: process.env.NEXT_PUBLIC_APPID,
messagingSenderId: process.env.NEXT_PUBLIC_MESSAGING_SENDER_ID,
measurementId: process.env.NEXT_PUBLIC_MEASUREMENT_ID,

grizzled osprey
#

how would hte .env look like?

#

also is it .env.local or \just .env

smoky thorn
#

Both will work fine.

smoky thorn
#

@grizzled osprey

grizzled osprey
smoky thorn
#

issue related to the date

grizzled osprey
#

would you pleaes elaborate

smoky thorn
#

starting and ending dates inside courses and experiences

grizzled osprey
#

whats wrong with them?

smoky thorn
#

try to pass in another way

grizzled osprey
#

do you mean that the complexity of the object returned by the Date Picker component is what's causing the entire thing to crash?

smoky thorn
#

right

#

try to pass a string

grizzled osprey
#

sure thing. I'll look into it. I just finished my workout, so i'll fix me a bite and get back with my "test results"

final linden
grizzled osprey
#

I'm checking it now, and it seems to be working after commenting out everything datepicker related

grizzled osprey
grizzled osprey
# smoky thorn try to pass a string

but the thing is, with the date pickers, I need the exact data is gives me, but at the same time I need the " simplified" version for the doc posting to go through.

The way I picture me doing it, would probably result in some spaghetti code. Is there a "neater" version of doing it that looks concise and nice?

#

I already plan on tidying up the code a little bit, by making the logic of "uploading reseme to storage".

smoky thorn
#

<DatePicker
defaultValue={dayjs(
props?.data?.selectedDate
? props?.data?.selectedDate
: new Date()
)}
className="w-100"
onChange={(e) => {
dispatch(setDate({ value: e.toString() }));
}}
/>

#

@grizzled osprey

grizzled osprey
#

I see you're using dispatch, are you using useReducer with Tailwind

smoky thorn
#

only dispatch

#

redux & redux toolkit

grizzled osprey
#

               defaultValue={dayjs(
                  props?.data?.selectedDate
                    ? props?.data?.selectedDate
                    : new Date()
                )}

So this checks if there's a set date, and if there's none, we call th Date class?

                onChange={(e) => {
                  dispatch(setDate({ value: e.toString() }));
                }}

and here, we transform the entire object returned by the datepicker into string?

smoky thorn
#

onChange={(e) => {
dispatch(setDate({ value: e.toString() }));
}}

return shown in above ss

#

<DatePicker
{...field}
label="End Date"
value={field.value || null}
onChange={(e) => {
form.setFieldValue(
courses.${index}.endDate,
e.toString()
);
}}
sx={{ width: "100%" }}
/>

grizzled osprey
#

apologies, I'm a little confused, but allow me to explain,

<DatePicker
  {...field}
  label="Starting From"
  value={field.value || null}
  onChange={(date) => {
    form.setFieldValue(
      `courses.${index}.startDate`,
      date
    );
  }}
  sx={{ width: "100%" }}
/>

this is my datepicker component, that sets the data to Formik's values via setFieldValue, and the object being set is this


"$D": 27
โ€‹โ€‹โ€‹โ€‹
"$H": 0
โ€‹โ€‹โ€‹โ€‹
"$L": "en"
โ€‹โ€‹โ€‹โ€‹
"$M": 11
โ€‹โ€‹โ€‹โ€‹
"$W": 3
โ€‹โ€‹โ€‹โ€‹
"$d": Date Wed Dec 27 2023 00:00:00 GMT+0300 (GMT+03:00)

"$isDayjsObject": true
โ€‹โ€‹โ€‹โ€‹
"$m": 0
โ€‹โ€‹โ€‹โ€‹
"$ms": 0
โ€‹โ€‹โ€‹โ€‹
"$s": 0
โ€‹โ€‹โ€‹โ€‹
"$u": undefined
โ€‹โ€‹โ€‹โ€‹
"$x": Object {  }
โ€‹โ€‹โ€‹โ€‹
"$y": 2023
โ€‹โ€‹โ€‹โ€‹

so to get the same result shown in the screenshot, am i to get the "$d"?

smoky thorn
#

firebase not not accept "$u": undefined

grizzled osprey
#

but if I do that from the Formik form, I need to populate the same datepicker in the "preview" stage, is that data enough to do taht?

grizzled osprey
#

but then the entire form crashed

smoky thorn
#

listen

#

change the picked date into string

#

to show default value and value use
import dayjs from "dayjs";
dayjs(
selectedDate
)

grizzled osprey
#

okay i'll try it now. it's my first time dealing with date pickers, so it's a little confusing

vocal portal
#

I would recommend you guys to fork @grizzled osprey 's repo and create a PR on it that you can share instead of bombing poor man with 100 messages. It is very hard to follow multiple conversations in a single thread. You can describe the problem in the PR/Issue and leave some comments in your code as well.

It would be very helpful for both sides as you will practice how to create PR/Issue, how to create meaningful commits, how to describe an issue and propose a solution for that. The other side will be able to practice "code review".

Both sides will get more familiar with github as well and how a version controller works.

grizzled osprey
grizzled osprey
vocal portal
grizzled osprey
vocal portal
grizzled osprey
grizzled osprey
#

Many thanks to @smoky thorn and @final linden for their help in this task. Datepicker values seem to have caused the problem. And I think it's fixed now. Feel free to check my latest commit

vocal portal
grizzled osprey
smoky thorn
#

@grizzled osprey Is your problem resolved?

grizzled osprey
smoky thorn
#

I have some suggestions for your project.

grizzled osprey
#

please go ahead

smoky thorn
#

1.Use the React form hook.

#

If you are going to work more deeply into this project

#

use firebase functions

grizzled osprey
grizzled osprey
smoky thorn
#

Try to use an env file.

grizzled osprey
smoky thorn
grizzled osprey
#

Oh yea i'm planning on doin that. After i'm done with this project, i'm headed off to learn how to properly fullstack with Next.js

vocal portal
vocal portal
grizzled osprey
grizzled osprey
grizzled osprey
#

I got another problem to report guys ๐Ÿ˜…

For some reason, after refreshing the page, i need to press the "submit" button twice for the document to be sent to firestore (on the second press).

I'm a little confused as why this is happening

vocal portal
grizzled osprey
#

I decided to move the submitting logic into the children component and i think it solved things