对于想了解如何使用Firestore更新“对象数组”?的读者,本文将提供新的信息,并且为您提供关于angularjs–Firestore–使用数组添加对象、FirebaseFirestoreonSna
对于想了解如何使用Firestore更新“对象数组”?的读者,本文将提供新的信息,并且为您提供关于angularjs – Firestore – 使用数组添加对象、Firebase Firestore onSnapshot 在使用 Firestore FieldValue for Timestamp 时为空、firebase – 使用Flutter将对象添加到Cloud Firestore、Firestore 错误 - firebase:emulator:start - Firestore 模拟器已退出,代码:1的有价值信息。
本文目录一览:- 如何使用Firestore更新“对象数组”?
- angularjs – Firestore – 使用数组添加对象
- Firebase Firestore onSnapshot 在使用 Firestore FieldValue for Timestamp 时为空
- firebase – 使用Flutter将对象添加到Cloud Firestore
- Firestore 错误 - firebase:emulator:start - Firestore 模拟器已退出,代码:1
如何使用Firestore更新“对象数组”?
我目前正在尝试使用Firestore,但遇到了非常简单的问题:“更新数组(又称子文档)”。
我的数据库结构非常简单。例如:
proprietary: "John Doe",sharedWith: [ {who: "first@test.com", when:timestamp}, {who: "another@test.com", when:timestamp}, ],
我正在尝试(没有成功)将新记录推入shareWith
对象数组。
我试过了:
// With SETfirebase.firestore().collection(''proprietary'').doc(docID).set( { sharedWith: [{ who: "third@test.com", when: new Date() }] }, { merge: true })// With UPDATEfirebase.firestore().collection(''proprietary'').doc(docID).update({ sharedWith: [{ who: "third@test.com", when: new Date() }] })
没有效果。这些查询将覆盖我的数组。
答案可能很简单,但我找不到它…
答案1
小编典典当前无法在Cloud Firestore中更新单个数组元素(或添加/删除单个元素)。
这段代码在这里:
firebase.firestore().collection(''proprietary'').doc(docID).set( { sharedWith: [{ who: "third@test.com", when: new Date() }] }, { merge: true })
这是说,在设置文件proprietary/docID
,使得sharedWith=[{who:"third@test.com",when:newDate()}
但是不会影响现有的文档属性。这与update()
您提供的set()
呼叫非常相似,但是,如果文档不存在,则创建文档的update()
呼叫将失败。
因此,您有两种选择来实现所需的目标。
选项1-设置整个数组
调用set()
数组的全部内容,这将需要首先从DB中读取当前数据。如果您担心并发更新,则可以在事务中完成所有这些操作。
选项2-使用子集合
您可以sharedWith
对主文档进行子集合。然后添加单个项目将如下所示:
firebase.firestore() .collection(''proprietary'') .doc(docID) .collection(''sharedWith'') .add({ who: "third@test.com", when: new Date() })
当然,这带来了新的限制。您将无法基于共享对象来查询文档,也无法sharedWith
通过单个操作获取文档和所有数据。
angularjs – Firestore – 使用数组添加对象
export class Department { articals?: Artical[]=[]; moms?: number; id?: string; constructor() { } }
和:
export class Artical { moms?: number; price: number; name?: string; constructor() { } }
喜欢你可以看到部门包含一个’Articals’数组
所以我正在使用新的articals(及其属性)构建新的部门,并尝试将其添加到fireStore:
this.departmetsCollection.doc(departmentId).set(Object.assign({},myDepartment));
但得到错误:
“使用无效数据调用函数DocumentReference.set().不支持的字段值:自定义Artical对象”
P.s tryed仅将部分艺术品添加到部门的路径中
doc(departmentId).set({'articals':myArticals});
并且使用’update’而不是’set’但是没有任何帮助:S
将这种对象添加到firestore是什么意思?
解决方法
首先更改您的Department类:
export class Department { articals?: Array<any>=[]; moms?: number; id?: string; constructor() { } }
第二个映射你的“Artical”数组:
var map = arrayOfArtical.map((obj)=> {return Object.assign({},obj)});
然后你可以将地图值归因于’this.myDepartment.articals’
this.myDepartment.articals = map;
之后你可以执行:
this.departmetsCollection.doc(departmentId).set(Object.assign({},myDepartment))
Firebase Firestore onSnapshot 在使用 Firestore FieldValue for Timestamp 时为空
这是由于 Firebase 调用的 "latency compensation":
您应用中的本地写入将立即调用快照侦听器。 这是因为一个称为“延迟补偿”的重要功能。 当您执行写入时,您的听众将收到新的通知 数据发送到后端之前的数据。
你的快照是在离开你的应用之前触发的,时间戳字段为null
,然后在到达后端并设置时间戳后再次触发。
要区分两者,请使用文档中所述的字段 metadata.hasPendingWrites
。
firebase – 使用Flutter将对象添加到Cloud Firestore
我已经做了一个回复课:
class Reply { Reply(this.replyName,this.replyText,this.replyVotes); final String replyName; final String replyText; final String replyVotes; String getName() { return replyName; } String getText() { return replyText; } String getVotes() { return replyVotes; } }
如何向云Firestore添加Reply对象?
编辑:
为了澄清,我想创建一个数据类型为Object的字段,其中包含字段:Reply Object Image
解决方法
import 'package:Meta/Meta.dart'; class Replies { final String title; final Map coordinates; Replies({ @required this.title,@required this.coordinates,}); Map<String,dynamic> toJson() => { 'title': title,'coordinates': coordinates,}; }
并使您想要成为对象类型Map的字段.然后,在您要插入数据库的页面上,导入dbSchema.dart并创建一个新模型:
Replies _replyObj = new Replies( title: _topic,coordinates: _coordinates,);
这假设你在此之前定义了你的本地_coordinates(或其他)对象,例如:
_coordinates = { 'lat': '40.0000','lng': '110.000',};
然后插入到Firestore中,添加对象的toJson方法(不能插入/更新普通的Dart模型):
CollectionReference dbReplies = Firestore.instance.collection('replies'); Firestore.instance.runTransaction((Transaction tx) async { var _result = await dbReplies.add(_replyObj.toJson()); ....
Firestore 错误 - firebase:emulator:start - Firestore 模拟器已退出,代码:1
如何解决Firestore 错误 - firebase:emulator:start - Firestore 模拟器已退出,代码:1?
我在使用 angular 11 应用程序,以前我有一个过时的 Java 版本,但即使在更新它并重新启动电脑后,我在尝试启动本地 firestore 模拟器时仍然收到错误消息。
我已经完成了:
- npm i firebase @angular/fire
- 在我的 angular 应用程序中添加了必要的配置/修改
- firebase init(用于 Firestore、托管和 模拟器)
- 在 firebase google 控制台上创建了一个 firestore 数据库
- 更新到 Java sdk 16
有谁知道可能是什么问题?在过去的 2 个小时里,我一直在试图弄清楚。
Node 和 npm 版本
node - v15.12.0
npm - 7.6.3
我的java版本
java version "16" 2021-03-16
Java(TM) SE Runtime Environment (build 16+36-2231)
Java HotSpot(TM) 64-Bit Server VM (build 16+36-2231,mixed mode,sharing)
firebase-debug.log(firestore-debug.log 总是空的)
[debug] [2021-04-04T09:06:47.737Z] ----------------------------------------------------------------------
[debug] [2021-04-04T09:06:47.739Z] Command: C:\PROGRA~1\nodejs\node.exe C:\Users\Dev\AppData\Roaming\npm\node_modules\firebase-tools\lib\bin\firebase.js --debug emulators:start
[debug] [2021-04-04T09:06:47.739Z] CLI Version: 9.8.0
[debug] [2021-04-04T09:06:47.739Z] Platform: win32
[debug] [2021-04-04T09:06:47.739Z] Node Version: v15.12.0
[debug] [2021-04-04T09:06:47.740Z] Time: Sun Apr 04 2021 12:06:47 GMT+0300 (Eastern European Summer Time)
[debug] [2021-04-04T09:06:47.740Z] ----------------------------------------------------------------------
[debug]
[debug] [2021-04-04T09:06:47.745Z] > command requires scopes: ["email","openid","https://www.googleapis.com/auth/cloudplatformprojects.readonly","https://www.googleapis.com/auth/firebase","https://www.googleapis.com/auth/cloud-platform"]
[debug] [2021-04-04T09:06:47.747Z] > authorizing via signed-in user
[info] i emulators: Starting emulators: firestore,hosting {"Metadata":{"emulator":{"name":"hub"},"message":"Starting emulators: firestore,hosting"}}
[debug] [2021-04-04T09:06:47.762Z] [hub] writing locator at C:\Users\Dev\AppData\Local\Temp\hub-my-coffee-app-2.json
[debug] [2021-04-04T09:06:47.776Z] Ignoring unsupported arg: projectId {"Metadata":{"emulator":{"name":"firestore"},"message":"Ignoring unsupported arg: projectId"}}
[debug] [2021-04-04T09:06:47.777Z] Ignoring unsupported arg: auto_download {"Metadata":{"emulator":{"name":"firestore"},"message":"Ignoring unsupported arg: auto_download"}}
[debug] [2021-04-04T09:06:47.777Z] Starting Firestore Emulator with command {"binary":"java","args":["-Dgoogle.cloud_firestore.debug_log_level=FINE","-Duser.language=en","-jar","C:\\Users\\Dev\\.cache\\firebase\\emulators\\cloud-firestore-emulator-v1.11.12.jar","--host","127.0.0.1","--port",8080,"--rules","D:\\_Repos ? ?\\my-coffee-app-pwa\\my-coffee-app-web\\firestore.rules"],"optionalArgs":["port","webchannel_port","host","rules","functions_emulator","seed_from_export"],"joinArgs":false} {"Metadata":{"emulator":{"name":"firestore"},"message":"Starting Firestore Emulator with command {\"binary\":\"java\",\"args\":[\"-Dgoogle.cloud_firestore.debug_log_level=FINE\",\"-Duser.language=en\",\"-jar\",\"C:\\\\Users\\\\Dev\\\\.cache\\\\firebase\\\\emulators\\\\cloud-firestore-emulator-v1.11.12.jar\",\"--host\",\"127.0.0.1\",\"--port\",\"--rules\",\"D:\\\\_Repos ? ?\\\\my-coffee-app-pwa\\\\my-coffee-app-web\\\\firestore.rules\"],\"optionalArgs\":[\"port\",\"webchannel_port\",\"host\",\"rules\",\"functions_emulator\",\"seed_from_export\"],\"joinArgs\":false}"}}
[info] i firestore: Firestore Emulator logging to firestore-debug.log {"Metadata":{"emulator":{"name":"firestore"},"message":"Firestore Emulator logging to \u001b[1mfirestore-debug.log\u001b[22m"}}
[warn] ! firestore: Fatal error occurred:
Firestore Emulator has exited with code: 1,stopping all running emulators {"Metadata":{"emulator":{"name":"firestore"},"message":"Fatal error occurred: \n Firestore Emulator has exited with code: 1,\n stopping all running emulators"}}
[info] i firestore: Stopping Firestore Emulator {"Metadata":{"emulator":{"name":"firestore"},"message":"Stopping Firestore Emulator"}}
[debug] [2021-04-04T09:06:52.112Z] Firestore Emulator: Unable to terminate process (PID=16940) {"Metadata":{"emulator":{"name":"firestore"},"message":"Firestore Emulator: Unable to terminate process (PID=16940)"}}
[warn] ! firestore: Error stopping Firestore Emulator {"Metadata":{"emulator":{"name":"firestore"},"message":"Error stopping Firestore Emulator"}}
[info] i hub: Stopping emulator hub {"Metadata":{"emulator":{"name":"hub"},"message":"Stopping emulator hub"}}
我的 package.json
{
"name": "my-coffee-app","version": "0.0.0","scripts": {
"ng": "ng","start": "ng serve","build": "ng build","test": "ng test","lint": "ng lint","e2e": "ng e2e"
},"private": true,"dependencies": {
"@angular/animations": "~11.2.5","@angular/cdk": "11.2.5","@angular/common": "~11.2.5","@angular/compiler": "~11.2.5","@angular/core": "~11.2.5","@angular/fire": "^6.1.4","@angular/forms": "~11.2.5","@angular/material": "11.2.5","@angular/platform-browser": "~11.2.5","@angular/platform-browser-dynamic": "~11.2.5","@angular/router": "~11.2.5","@angular/service-worker": "^11.2.5","@ngx-formly/core": "^5.0.0","@ngx-formly/material": "^5.0.0","@ngx-formly/schematics": "^5.10.15","bootstrap": "^5.0.0-beta3","firebase": "^8.3.2","material-theme-creator": "^3.0.7","rxjs": "~6.6.0","tslib": "^2.0.0","zone.js": "~0.11.3"
},"devDependencies": {
"@angular-devkit/architect": ">= 0.900 < 0.1200","@angular-devkit/build-angular": "~0.1102.3","@angular/cli": "~11.2.4","@angular/compiler-cli": "~11.2.5","@types/jasmine": "~3.6.0","@types/node": "^12.11.1","codelyzer": "^6.0.0","firebase-tools": "^9.8.0","fuzzy": "^0.1.3","inquirer": "^6.2.2","inquirer-autocomplete-prompt": "^1.0.1","jasmine-core": "~3.6.0","jasmine-spec-reporter": "~5.0.0","karma": "~6.1.0","karma-chrome-launcher": "~3.1.0","karma-coverage": "~2.0.3","karma-jasmine": "~4.0.0","karma-jasmine-html-reporter": "^1.5.0","open": "^7.0.3","protractor": "~7.0.0","ts-node": "~8.3.0","tslint": "~6.1.0","typescript": "~4.1.5"
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)
今天的关于如何使用Firestore更新“对象数组”?的分享已经结束,谢谢您的关注,如果想了解更多关于angularjs – Firestore – 使用数组添加对象、Firebase Firestore onSnapshot 在使用 Firestore FieldValue for Timestamp 时为空、firebase – 使用Flutter将对象添加到Cloud Firestore、Firestore 错误 - firebase:emulator:start - Firestore 模拟器已退出,代码:1的相关知识,请在本站进行查询。
本文标签: