vors
vorsβ€’3y ago

I have a really stupid problem that I

I have a really stupid problem that I cannot solve in the last hour. I'm trying to use result of image mutation in another action Here is the mutation
export default mutation(async ({ db, auth }, project: Id<"projects">, url: string): Promise<Document<"images">> => {
const user = await getCurrentUser(db, auth);
const id = await db.insert("images", {
project: project,
url: url,
});
const val = await db.get(id);
if (val == null) {
throw new Error(`Was not able to query the image ${id} after inserting`)
}
return val;
});
export default mutation(async ({ db, auth }, project: Id<"projects">, url: string): Promise<Document<"images">> => {
const user = await getCurrentUser(db, auth);
const id = await db.insert("images", {
project: project,
url: url,
});
const val = await db.get(id);
if (val == null) {
throw new Error(`Was not able to query the image ${id} after inserting`)
}
return val;
});
Here is the callsite code
var images = []
const image = await runMutation("newImage", project_id, "https://dummyimage.com/300.png/09f/fff");
images.push(image._id)
const generation = {
images: images,
}
const createdGeneration = await runMutation("storeGeneration", generation);
var images = []
const image = await runMutation("newImage", project_id, "https://dummyimage.com/300.png/09f/fff");
images.push(image._id)
const generation = {
images: images,
}
const createdGeneration = await runMutation("storeGeneration", generation);
I'm getting error
failure
Field name $id starts with a '$', which is reserved.
at validateObjectField (../../node_modules/convex/src/values/values.ts:259:2)
at convexToJsonInternal (../../node_modules/convex/src/values/values.ts:480:4)
at convexToJsonInternal (../../node_modules/convex/src/values/values.ts:481:2)
at <anonymous> (../../node_modules/convex/src/values/values.ts:433:4)
at convexToJsonInternal (../../node_modules/convex/src/values/values.ts:431:29)
at convexToJsonInternal (../../node_modules/convex/src/values/values.ts:481:2)
at <anonymous> (../../node_modules/convex/src/values/values.ts:433:4)
at convexToJsonInternal (../../node_modules/convex/src/values/values.ts:431:29)
failure
Field name $id starts with a '$', which is reserved.
at validateObjectField (../../node_modules/convex/src/values/values.ts:259:2)
at convexToJsonInternal (../../node_modules/convex/src/values/values.ts:480:4)
at convexToJsonInternal (../../node_modules/convex/src/values/values.ts:481:2)
at <anonymous> (../../node_modules/convex/src/values/values.ts:433:4)
at convexToJsonInternal (../../node_modules/convex/src/values/values.ts:431:29)
at convexToJsonInternal (../../node_modules/convex/src/values/values.ts:481:2)
at <anonymous> (../../node_modules/convex/src/values/values.ts:433:4)
at convexToJsonInternal (../../node_modules/convex/src/values/values.ts:431:29)
How I can pass the _id value that I got from one mutation into another through the action?
21 Replies
vors
vorsOPβ€’3y ago
I think what I observe is that I can pass _id as a scalar, but when I try to do that for an array, convex doesn't like it.
vors
vorsOPβ€’3y ago
I have read https://docs.convex.dev/using/document-ids#serializing-ids but I don't think it's covered there.
Document IDs | Convex Developer Hub
Create complex, relational data models using IDs.
vors
vorsOPβ€’3y ago
I tried using image._id.toString() and then trying to re-hydrate it into a real ID on the other side, but I'm just getting a string ["[object Object]"]
lee
leeβ€’3y ago
hi this looks like runMutation is not deserializing the json into an object correctly. What's the context of the callsite? is runMutation the result of a useMutation or is this within an action or http endpoint?
vors
vorsOPβ€’3y ago
withing the action ok so I solved (but I'd call it more like a workaround). What I needed to do is to de-hydrate my id inside the first runMutation and then pass it through the aciton as a string and the re-hydrate it into ID honestly that seems very non-intutive
lee
leeβ€’3y ago
interesting. this should be possible as you wrote it. i'm glad you found a workaround, but it seems like a bug on our side. i'll take a look
vors
vorsOPβ€’3y ago
thank you! I believe the root cause may be in the too strict checking of serialization of arrays
lee
leeβ€’3y ago
yep definitely a bug on our side. sorry about that. i'm sending the fix for review now, but it probably won't deploy today, so nice job finding a workaround πŸ™‚ for posterity (and to help construct workarounds), the issue is with the return values of queries and mutations when called from actions. simple objects, strings, arrays, and numbers should work. but complex types like Id, Set, Map, and bigint will not work until my fix deploys
sshader
sshaderβ€’3y ago
As a bit of an aside -- it looks like some of the code you pasted could be a single mutation? (but also maybe there's a bunch of other code you omitted to give us a nice minimal snippet of your issue)
vors
vorsOPβ€’3y ago
There is a side effect that I ommited Glad to hear you are on it!
ballingt
ballingtβ€’3y ago
@vors Convex version 0.9.1 is out, including @lee's fix! You can upgrade to it with npm install convex@0.9.1
vors
vorsOPβ€’3y ago
amazing nit: I think it should be npm install -g convex@0.9.1 ? let me see can I revert the workaround back
ballingt
ballingtβ€’3y ago
Not unless you want to install it globally! typically with npm you install things at a project level, like poetry or pip-env for Python
vors
vorsOPβ€’3y ago
hm something very weired happened
sergeivorobev@sergeis-air: ~/src/craitvt npm convex --version
9.4.1
sergeivorobev@sergeis-air: ~/src/craitvt npm convex --version
9.4.1
so I got afraid and instelled it globally πŸ™‚ that "fixed" it oh sorry should be npx ok still learning the ropes! Back to removing workaround awesome, confirming that it works now!!! πŸŽ‰
ballingt
ballingtβ€’3y ago
the important thing is that in the package.json in your project, it has a line like "convex": "^0.9.1", β€”Β that's the version that will be used in your project (if the library is installed in both places) Thanks for reporting the issue and sorry for the trouble!
vors
vorsOPβ€’3y ago
of course, thank you for the amazing community support πŸ™‚ πŸ™‡β€β™‚οΈ I have another error in the same veins
Error: [CONVEX A(actions/generate)] Invalid schedule request: {"code":"BadRequest","message":"Document(value: {_creationTime: 1676333212218.0454, _id: Id(_scheduled_jobs|RiYNSe7HoD1JkesWmfVLyg), nextTs: 1676333212197999954, state: {type: \"pending\"}, udfArgs: [{_creationTime: 1676333212106.5386, _id: Id(images|NTL-N_3kHu7RZxSucPlLyg), metadata: \"{}\", project: Id(projects|7F5jd_F8fA0AwIyTvwNLyQ), url: \"https://dummyimage.com/300.png/09f/fff\"}, \"123213\"], udfPath: \"actions/inference.js:default\"}) isn't a valid document:\n Field '_creationTime' starts with an underscore, which is not allowed for nested fields\n Field '_id' starts with an underscore, which is not allowed for nested fields"}
at async <anonymous> (../../convex/actions/generate.ts:47:1)
Error: [CONVEX A(actions/generate)] Invalid schedule request: {"code":"BadRequest","message":"Document(value: {_creationTime: 1676333212218.0454, _id: Id(_scheduled_jobs|RiYNSe7HoD1JkesWmfVLyg), nextTs: 1676333212197999954, state: {type: \"pending\"}, udfArgs: [{_creationTime: 1676333212106.5386, _id: Id(images|NTL-N_3kHu7RZxSucPlLyg), metadata: \"{}\", project: Id(projects|7F5jd_F8fA0AwIyTvwNLyQ), url: \"https://dummyimage.com/300.png/09f/fff\"}, \"123213\"], udfPath: \"actions/inference.js:default\"}) isn't a valid document:\n Field '_creationTime' starts with an underscore, which is not allowed for nested fields\n Field '_id' starts with an underscore, which is not allowed for nested fields"}
at async <anonymous> (../../convex/actions/generate.ts:47:1)
I'm trying to pass an object into a scheduler.runAfter
presley
presleyβ€’3y ago
Yeah, we try to serialize the arguments as documents internally, which results in that issue. As workaround, try to pass just the id of the document (and then you can fetch it back) or whatever fields you need. Will work on a fix.
vors
vorsOPβ€’3y ago
yeap did that... but seems clunky while I have a working object πŸ™‚
presley
presleyβ€’3y ago
haha. Yeah, I can only imagine how clunky it is. Working on a fix.
Presley
Presleyβ€’3y ago
Fixed has been deployed btw. Let us know if you still experience issues.
vors
vorsOPβ€’3y ago
Thank you!

Did you find this page helpful?