gpt4 book ai didi

typescript - 如何使用在 TypeScript 中编码的 CloudFunction 将 admin.firestore.FieldValue.serverTimestamp() 传递给 update() 方法

转载 作者:行者123 更新时间:2023-12-03 23:12:34 27 4
gpt4 key购买 nike

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

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() 不能在数组内使用”。

这是你做什么:

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

您可能需要更改数据模型,例如,用映射替换数组,如下所示:
const time = admin.firestore.FieldValue.serverTimestamp();
await amisA.update({
[`amis.${numeroSender}`] :
{
connaissanceABBA: connaissanceABBA,
version: version,
ts: firebase.firestore.FieldValue.serverTimestamp()
}
});
});

关于typescript - 如何使用在 TypeScript 中编码的 CloudFunction 将 admin.firestore.FieldValue.serverTimestamp() 传递给 update() 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56526348/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com