Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass admin.firestore.FieldValue.serverTimestamp() to the update() method, using CloudFunction coded in TypeScript

How to pass admin.firestore.FieldValue.serverTimestamp() to the update() method? I want to insert this in an array, like this:

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;
const time = admin.firestore.FieldValue.serverTimestamp();
await amisA.update({
            [`amis.${numeroSender}`] : [time,connaissanceABBA,version]
        });
});

but 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`).
at WriteBatch.update (/user_code/node_modules/firebase- 
admin/node_modules/@google-cloud/firestore/build/src/write- 
batch.js:367:23)
at DocumentReference.update (/user_code/node_modules/firebase- 
admin/node_modules/@google- 
cloud/firestore/build/src/reference.js:372:14)
at Object.<anonymous> (/user_code/lib/index.js:121:25)
at next (native)
at fulfilled (/user_code/lib/index.js:4:58)
at process._tickDomainCallback (internal/process/next_tick.js:135:7)
like image 423
Djiveradjane Canessane Avatar asked Dec 07 '22 12:12

Djiveradjane Canessane


1 Answers

Instead of admin.firestore.FieldValue.serverTimestamp(), you can use admin.firestore.Timestamp.now()

const time = admin.firestore.Timestamp.now();
await amisA.update({
        [`amis.${numeroSender}`] : 
           {
    connaissanceABBA: connaissanceABBA,
    version: version,
    ts: firebase.firestore.FieldValue.serverTimestamp()
  }
});
});
like image 148
Drew Avatar answered May 22 '23 10:05

Drew