I am attempting to insert a record and would like to utilize the Ref(Collection(), “”) feature so that when I query it later, the related record can be returned in full, but I’m struggling to find a way to insert or update the record with the Ref object.
Using: Next.js “fauna” SDK
If I try to set it directly in the JSON object, I receive:
ReferenceError: Ref is not defined
If I try to wrap it in single quotes, it inserts, but is now a string and not an actual Ref object.
I’ve also tried to use a combination of the faunadb SDK and perform a query to get the Ref object and pass it in to the JSON object, but that is inserting a blank object ({}).
data.workspace = sys_workspace.byId(“id_number”)
This returns “ReferenceError: sys_workspace is not defined”
data.workspace = fqlsys_workspace.byId("id_number")
This returns workspace: {}
data.workspace = fql([‘sys_workspace.byId(“id_number”)’])
This returns workspace: {}
I’ve also tried executing the fql query on sys_workspace then setting my variable to the response and that sets it to the full object instead of just the Reference element. Not sure what I’m doing wrong here.
Hi @kadence Are you still looking for support on this one?
Note that the fql function returns a representation of your query that is meant to be sent to the database. Printing the query as JSON to the console will return {} because it doesn’t have any public fields.
Perhaps what you are looking for is something more like the following?
Thank you for the assistance. The problem is really more with Typescript than Fauna, but a valid issue nonetheless.
I ended up setting the value to a string with a placeholder to indicate it requires special handling (e.g. “[ref]tablename.byId(‘id’)[ref]”) then when I convert the JSON to string using stringify, I search and replace the “[ref] and [ref]” values before submitting to the Fauna SDK. Not pretty, but it works.