replaceData()

Replace document data field contents.

Signature

replaceData(object: {*: any}): <Document>

Description

The replaceData() method replaces all data fields with object. Fields not present in object are removed unless their value is null.

Parameters

Parameter Type Required Description

object

Object

Yes

Object with replacement fields.

Return value

Type Description

Document

Document with replaced data fields.

Examples

Given the following document:

{
  id: "392886847463751746",
  coll: Product,
  ts: Time("2099-04-10T16:50:12.850Z"),
  name: "limes",
  description: "Organic, 1 ct",
  price: 0.65
}

Call replaceData() with id and coll fields in the replacement document object. These fields have the same name as reserved metadata fields.

Product.byId("392886847463751746")?.replaceData({
  id: "12345",
  coll: "Product",
  name: "limes",
  description: "Organic, 2 ct",
  price: 0.99,
  quantity: 50
})
{
  id: "392886847463751746",
  coll: Product,
  ts: Time("2099-04-10T16:55:51.046Z"),
  name: "limes",
  description: "Organic, 2 ct",
  price: 0.99,
  quantity: 50,
  data: {
    coll: "Product",
    id: "12345"
  }
}

Rather than return an error, replaceData() treats any field with a reserved name as a document field and nests it in the document’s data property.

Is this article helpful? 

Tell Fauna how the article can be improved:
Visit Fauna's forums or email docs@fauna.com

Thank you for your feedback!