db.insert return type
So I'm encountering the following error in my vscode:
Here's what I have:
here's my schema:
11 Replies
user is of type Document<"users"> and applicationId is of type GenericId<"applications"> where users, applications, and submissions are all database tables
what is the return type? maybe it's failing b/c you're returning GenericId<"submissions"> and you're trying to return the submission itself?
user and application are both GenericId types. Btw, if you have a schema, then you can use
Id<"users">
and Id<"applications">
: GenericId
is just for when you don't have a schema
the return type of db.insert should be something like Promise<GenericId<"submissions">>
which is making me think your error is from returning the id when you expect to be returning the document itselfoh i see! is there an easy way to get this query to return the submission document? or should i make a document using the parameters I passed into the db.insert?
also, just wondering, how did you know that was the issue?
oh nevermind, I see it-- I was reading the msg incorrectly! thanks
if i create it using the parameters, I still won't have the creationtime..
is there an easy way to get this query to return the submission document?You can always use
db.get
to turn the ID into a document:
ah so, then I get this...
though this should never return null, because I just put it in the db; I'm a bit new to typescript so I'm not super sure how to resolve this
did this
let me know if there's a better fix! thanks!
Hmm. That shouldn't ever happen - can you reproduce it? If you could print the
_id
after the insert, I'd be curious if id is well formed & points to actual data@ian it's probably because
.get()
will return obj | null
b/c it can't guarantee the object is present -- the typescript type system can't
@ansa since you just inserted the document, it's safe to do db.get(...)!;
there. the !
means I know it's not null, compiler trust meah, so it won't happen at runtime, just the type error. that makes sense. I got confused and thought this was a runtime exception
gotcha! thanks!
right, not a runtime error, ts error
yep
@ansa no problem!
Oh duh. Sorry for not checking the types on my snippet!
The nulls returned from
db.get
are kinda obnoxious, but perhaps unavoidable given that we allow document deletion.
This conversation also makes me wonder if we should flip back to db.insert
returning the entire document, not just the ID (this is an option now that db.insert
is async). Although in many cases using the ID is better so that you can get a reactive version of the document from useQuery
.