简体   繁体   English

如何将非持久值发送到 Cloud Firestore?

[英]How to send non-persistent value to Cloud Firestore?

I want to send some value for a field to Cloud Firestore , but I dont want to be persist(saved) in Cloud Firestore .我想将某个value for a field发送到Cloud Firestore ,但我不想在Cloud Firestorepersist(saved)

Code:代码:

const message = {
  persistentData: {
     id: 'dSXYdieiwoDUEUWOssd',
     text: 'Hi dear how are you',
     date: new Date();
  },

  nonPersistentData: {
    securityCode: 393929949
  }
};

db.collection('messages').doc(message.persistentData.id).set(message).catch(e => {});

In above code I want to persit (save) persistentData , but I dont want to save nonPersistentData online nor offline , because I only need them to check real data in Firestore rule .在上面的代码中,我想persit (save) persistentData ,但我不想online nor offline save nonPersistentData ,因为我只需要它们检查Firestore rule中的真实数据。 So I dont want they should be accessible in cache(offline) or server(online) ...所以我不希望它们accessiblecache(offline) or server(online)中访问...

This is simply not possible with firestore.这对于 Firestore 来说根本不可能。 There is a similar question here . 这里有一个类似的问题。 You need to separate the data into public (persistent) and private data (non-persistent).您需要将数据分为公共(持久)和私有数据(非持久)。 One possible solution will be-一种可能的解决方案是 -

  1. From the client, push the private data which contains the securityCode to a new collection called securityCodes and store the id of the new entry.从客户端,将包含 securityCode 的私有数据推送到一个名为 securityCodes 的新集合,并存储新条目的 ID。 Because you don't want this info to be available to anyone, you can add a security rule因为您不希望任何人都可以使用此信息,所以您可以添加安全规则
match /securityCodes/{securityCode} {
  // No one can read the value from this collection, but only create
  allow create: true;
}
  1. In your public data, add the id of the previously added document在您的公共数据中,添加先前添加的文档的 id
data = {
  id: 'dSXYdieiwoDUEUWOssd',
  text: 'Hi dear how are you',
  date: new Date(),
  securityId: <id of the secretCode entry>
}
  1. In your security rules, get the secret code using the securityId you are sending with the public data.在您的安全规则中,使用您与公共数据一起发送的 securityId 获取密码。 Example-例子-
match /collectionId/documentId {
  allow create: if get(/secretCodes/$(request.resource.data.secretId)) == 'someknowncode'
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Cloud Firestore 中查询不存在的文档键 - How to query Cloud Firestore for non-existing keys of documents 如何在 Cloud Firestore 的数组中增加 map 中的值? - How to increment a value in a map inside an array in Cloud Firestore? 使用 Cloud Functions 访问 firestore 中的用户数据,但如何安全地发送 UID/IDToken? - Accessing user data in firestore using Cloud Functions, but how do I securely send the UID/IDToken? 使用 Cloud Firestore REST API 如何使用 http package 和 http.post() 方法发送数据 - Using Cloud Firestore REST API how to send data using http package with http.post() method 在 Vue 中使用云 Firestore 更新 boolean 值 - Updating boolean value with cloud firestore in Vue 如何检查 Cloud Firestore 中任何文档的集合中是否存在值(例如名称)? - How can I check if a value e.g. name exists in a collection within any of documents in Cloud Firestore? 从 Cloud Firestore 获取特定数据值 - get specific value of Data from cloud firestore 如何在 Flutter web 应用程序中使用云 Firestore - How to use cloud firestore in Flutter web app 如何在 Cloud Firestore 文档中列出子集合 - How to list subcollections in a Cloud Firestore document 如何在 Cloud Firestore 中使用逻辑 OR 执行复合查询? - How to perform compound queries with logical OR in Cloud Firestore?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM