本文介绍了如何使用TypeScript中编码的CloudFunction将admin.firestore.FieldValue.serverTimestamp()传递给update()方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将admin.firestore.FieldValue.serverTimestamp()传递给update()方法?我想将其插入到数组中,如下所示:

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]
        });
});

但我收到此错误

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)

推荐答案

如您收到的错误消息中所述,"FieldValue.serverTimestamp()不能在数组内部使用".

As detailed in the error message you receive, "FieldValue.serverTimestamp() cannot be used inside of an array".

您要使用的是什么

const time = admin.firestore.FieldValue.serverTimestamp();
await amisA.update({
            [`amis.${numeroSender}`] : [time,connaissanceABBA,version]
        });
});

您可能必须更改数据模型,例如,用地图替换数组,如下所示:

You might have to change your data model and, for example, replace your array by a map, as follows:

const time = admin.firestore.FieldValue.serverTimestamp();
await amisA.update({
            [`amis.${numeroSender}`] : 
               {
        connaissanceABBA: connaissanceABBA,
        version: version,
        ts: firebase.firestore.FieldValue.serverTimestamp()
      }
    });
});

这篇关于如何使用TypeScript中编码的CloudFunction将admin.firestore.FieldValue.serverTimestamp()传递给update()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 02:18