3

I want to update a map in Firestore using cloud Function typed in Typescript, so I made this code but it doesn't work:

import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';

admin.initializeApp(functions.config().firebase);
exports.sendNote = functions.https.onCall(async(data,context)=>{
const numeroSender: string = data['numeroSender'];
const amisA = admin.firestore().collection('Amis').doc(numeroReceiver);
const connaissanceABBA:number = 3.0;
const version:number = 1;
await amisA.update({
           'amis.${numeroSender}' : [connaissanceABBA,version]
        });
});

the editor doesn't replace numeroSender by its value, and I tried also with:

'amis.'+numeroSender

and the editor is asking a semicolon and show many other errors. Even I tried to try this way:

const mapA: string = ("amis."+numeroSender);
console.log(mapA);
await amisA.update({
            mapA : [connaissanceABBA,version]
        });

the console shows the correct string, but update function doesn't read mapA value but executes with string 'mapA'.

1 Answer 1

5

You can do:

await amisA.update({
      [`amis.${numeroSender}`] : [connaissanceABBA, version]
});

In ES6 (and therefore in TypeScript as well) you can use ComputedPropertyName as shown in the Language Specification: http://www.ecma-international.org/ecma-262/6.0/#sec-object-initializer


Note that in ES5 you would have to do as follows, using the square brackets notation:

var obj = {};
obj[`amis.${numeroSender}`] = [connaissanceABBA, version]; 
amisA.update(obj);

Note also that this will work as expected with the update() method since "fields can contain dots to reference nested fields within the document". However, the resulting object in the JavaScript code may not be exactly what you expect. You can see it by doing:

var obj1 = {
          [`amis.${numeroSender}`] : [connaissanceABBA, version]
    }
console.log(JSON.stringify(obj1));

Finally note that template literals are enclosed by back-ticks, like:

`amis.${numeroSender}`

and not like:

'amis.${numeroSender}'

as shown in your question.

Sign up to request clarification or add additional context in comments.

3 Comments

You made my day! I have another issue. When I try to insert admin.firestore.FieldValue.serverTimestamp() in the value array or via a const, I get this error :**Error: Update() requires either a single JavaScript object or an alternating list of field/value pairs that can be followed by an optional precondition. Value for argument "dataOrField" is not a valid Firestore value. FieldValue.serverTimestamp() cannot be used inside of an array (found in field amis.+33651177261.0).**
Can you write a brand new question showing exactly how do you pass admin.firestore.FieldValue.serverTimestamp() to the update() method. Apparently you get an error "FieldValue.serverTimestamp() cannot be used inside of an array "
@JeevaCanessane See my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.