newId() cannot be used directly as an id

This might be by design, but felt unexpected nonetheless. If for some reason you would like to create an id with the newId() method this cannot be used directly as the value for the id of a new document.

let id = newId()
testCollection.create({
  id: id,
  created: Time.now()
})

It will return a type error:

invalid_type

error: Value at `id` has type ID, which cannot be persisted.

If you would add it as a string, it does work:

let id = newId()
testCollection.create({
  id: "#{id}",
  created: Time.now()
})

Should this be the case? Is this something that needs more detailed documentation in that case?

Thank you!

That is odd! You should be able to use an ID as an ID! The error is otherwise correct in that you cannot persist IDs.

Note that the global function newId returns an instance of the ID type, not a string. Our docs need updated to reflect this.

You don’t need to use interpolated strings. IDs have a toString() method.

let id = newId().toString()
testCollection.create({
  id: id,
  created: Time.now()
})

But why?

haha I do have to ask why you are doing this though. You do not need to provide an id when creating a document. If you don’t one will be provided, and the service that issues IDs when you use newId is the same one that provides default ids.

The above is equivalent to just this

testCollection.create({
  created: Time.now()
})

Thanks for your response!

Just trying things in FQL v10 to get familiar with it.

Awesome. Well, thank you for reporting!

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.