user3487545
user3487545

Reputation: 21

How to update DocumentReference?

I tried to update DocumentReference but could not do it.

update() method fails. How to use it? (How to pass argument?)

firebase-admin version is 6.3.0.
@google-cloud/firestore version is 0.19.0.

❯ firebase functions:shell
i  functions: Preparing to emulate functions.
Warning: You're using Node.js v8.14.0 but Google Cloud Functions only supports v6.11.5.
✔  functions: sampleFunc
firebase > const admin = require('firebase-admin');
firebase > admin.initializeApp();
firebase > let ref = admin.firestore().collection("users").doc('edqupYQhzqV1ODjEpoJn');
firebase > let updates = { email: '[email protected]' };
firebase > ref.update(updates).then(value => console.log(value) );
Error: Update() requires either a single JavaScript object or an alternating list of field/value pairs that can be followed by an optional precondition. Argument "dataOrField" is not a valid Document. Input is not a plain JavaScript object.
    at WriteBatch.update (/Users/xxx/Desktop/sample-functions/functions/node_modules/@google-cloud/firestore/build/src/write-batch.js:359:23)
    at DocumentReference.update (/Users/xxx/Desktop/sample-functions/functions/node_modules/@google-cloud/firestore/build/src/reference.js:387:14)

Update
Document has already been created, so get() works.

firebase > ref.get().then(snapshot => console.log(snapshot.data()));

Errors also occur in set() as well.

firebase > ref.set({email: '[email protected]'}, {merge: true}).then(value => console.log(value));
Error: Argument "data" is not a valid Document. Input is not a plain JavaScript object.
    at Validator.(anonymous function).values [as isDocument] (/Users/xxx/Desktop/sample-functions/functions/node_modules/@google-cloud/firestore/build/src/validate.js:99:27)
    at WriteBatch.set (/Users/xxx/Desktop/sample-functions/functions/node_modules/@google-cloud/firestore/build/src/write-batch.js:232:25)
    at DocumentReference.set (/Users/xxx/Desktop/sample-functions/functions/node_modules/@google-cloud/firestore/build/src/reference.js:349:27)

Upvotes: 2

Views: 4561

Answers (3)

Ronnie Smith
Ronnie Smith

Reputation: 18595

create an object first then assign your values to a field/property on that object like:

var myObj = {};
myObj["myfield"] = myvalue;

now pass as the 2nd argument of .set or .update.

Upvotes: 0

Troels Lenda
Troels Lenda

Reputation: 524

I've never tried using the Admin SDK in the functions:shell like you do in your example. However I can easily reproduce your error. I guess that the functions:shell somehow tampers with your variable updates. When i log updates undefined is concatenated in output

firebase > console.log(updates)
{ email2: '[email protected]' }
undefined

If you put your code in a JS file, initialize your app with a service account and run in with node it will most likely work just fine!

Read more about how to use Service Account

Upvotes: 0

Marcio Cabral
Marcio Cabral

Reputation: 75

Following the example you posted, you are trying to update a document that does not exist. In this case you should create it first.

If you're not sure whether the document exists, pass the option to merge the new data with any existing document to avoid overwriting entire documents.

From Google's Documentation:

var cityRef = db.collection('cities').doc('BJ');

var setWithOptions = cityRef.set({
  capital: true
}, {merge: true});

Reference: Firestore Add Data

Upvotes: 3

Related Questions