我正在使用AngularFire并在我的中启用了离线支持的持久性app.module.ts:
imports: [
AngularFireModule.initializeApp(environment.firebase),
AngularFirestoreModule.enablePersistence()
]
Run Code Online (Sandbox Code Playgroud)
valueChanges()当我的应用程序启动时,我使用如下方式检索用户:
const doc: AngularFirestoreDocument<User> = this.myCollection.doc<User>(userId);
doc.valueChanges().pipe(take(1)).subscribe((user: User) => {
console.log(user);
}, (err) => {
console.err(err);
});
Run Code Online (Sandbox Code Playgroud)
对我来说奇怪的是,如果我将来自其他来源的修改作为应用程序应用于数据库,例如,如果我将自己登录到 Google Firestore 控制台并手动修改用户数据中的值,则不会获取这些数据。
如果我重新启动应用程序,它将始终获取本地保留的数据,并且永远不会注意到这些值已更改。
如果我分别删除持久性,则enablePersistence()可以正确获取数据。
你知道问题的原因是什么吗?
在这种情况下我不应该使用吗valueChanges()?
或者发生这种情况是因为我只获取该值一次(请参阅take(1)),因此永远不会获取更新的值?
提前感谢您的帮助
我正在尝试 Angularfire 和他们的身份验证守卫。我使用他们的文档来学习如何设置我的身份验证防护。一切看起来都很完美,但后来我注意到在我的登录页面中,角度材质输入字段不再响应。并且控制台中没有错误消息。如果我删除 canActivate 防护,则角度材料输入可以工作。这里可能是什么问题,我是否错过了某些步骤或者我需要一些额外的设置?我创建了一个小型演示应用程序来测试它,它也有同样的问题。github 中的演示应用程序
应用程序路由.module.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import {LoginComponent} from './login/login.component';
import {canActivate, redirectLoggedInTo, redirectUnauthorizedTo} from '@angular/fire/auth-guard';
import {DashboardComponent} from './dashboard/dashboard.component';
const redirectLoggedInToDashboard = () => redirectLoggedInTo(['dashboard']);
const redirectUnauthorizedToLogin = () => redirectUnauthorizedTo(['login']);
const routes: Routes = [
{path: '', redirectTo: 'login', pathMatch: 'full'},
{path: 'login', component: LoginComponent, ...canActivate(redirectLoggedInToDashboard)},
{path: 'dashboard', component: DashboardComponent, ...canActivate(redirectUnauthorizedToLogin)},
{path: '**', redirectTo: 'login'}
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule] …Run Code Online (Sandbox Code Playgroud) timestamp我正在尝试使用将Firestore 中的对象转换为DateTypeScript 中的对象toDate()。
import { AngularFirestore } from '@angular/fire/firestore';
...
constructor(private database?: AngularFirestore) {...}
...
const someVariable = a.date.toDate();
Run Code Online (Sandbox Code Playgroud)
虽然该功能在开发模式下工作正常,但我收到 TypeScript 编译检查错误。ng prod此外,由于此错误,我无法构建产品版本(使用)。
a.date.toDate() ~~~~ src/app/project/some.service.ts:74:36 -
错误 TS2339:类型“Date”上不存在属性“toDate”。
有什么建议我该如何解决这个问题吗?
我开始新的 Ionic/Angular 项目并添加 @Angular/Fire 包。
当我运行“ionicserve”时,出现以下错误:
Error: node_modules/@angular/fire/compat/firestore/interfaces.d.ts:13:18 - error TS2430: Interface 'DocumentSnapshotExists<T>' incorrectly extends interface 'DocumentSnapshot<DocumentData>'.
The types returned by 'data(...)' are incompatible between these types.
Type 'T' is not assignable to type 'DocumentData | undefined'.
Type 'T' is not assignable to type 'DocumentData'.
13 export interface DocumentSnapshotExists<T> extends firebase.firestore.DocumentSnapshot {
node_modules/@angular/fire/compat/firestore/interfaces.d.ts:13:41
13 export interface DocumentSnapshotExists<T> extends firebase.firestore.DocumentSnapshot {
This type parameter might need an `extends firebase.firestore.DocumentData` constraint.
node_modules/@angular/fire/compat/firestore/interfaces.d.ts:13:41
13 export interface DocumentSnapshotExists<T> extends firebase.firestore.DocumentSnapshot {
This type parameter …Run Code Online (Sandbox Code Playgroud) 这是我现在的代码:
var url = 'https://*****.firebaseio.com/photos';
var promise = angularFire(url, $scope, 'photos', {});
promise.then(function() {
//limit here.
});
Run Code Online (Sandbox Code Playgroud)
我可以插入什么代码来限制从firebase返回的内容到只有具有指定优先级的内容(假设优先级为"new")?
谢谢您的帮助.
当我运行我的业力单元脚本并且我无法弄清楚原因时,我收到此错误
Error: [$injector:unpr] Unknown provider: FBURLProvider <- FBURL
Run Code Online (Sandbox Code Playgroud)
这是我的指令代码
'use strict';
angular.module('userMenu', ['firebase'])
.directive('userMenu', function (FBURL, angularFire) {
return {
restrict: 'A',
scope: true ,
link: function postLink(scope, element, attrs) {
/**
* Returns the logged in user information
* @param {string} FBURL
* @param {object} scope
* @returns {promise}
*/
scope.getUserDataFromFirebase = function(FBURL, scope) {
var ref = new Firebase(FBURL + '/users/' + scope.auth.id);
return angularFire(ref, scope, 'user', {})
}
}
};
});
Run Code Online (Sandbox Code Playgroud)
这是我的规范代码
'use strict';
describe('Directive: userMenu', function …Run Code Online (Sandbox Code Playgroud) 数组的通常.length属性不适用于firebase返回的数组.这是返回数组的console.log:
0: Object
1: Object
2: Object
3: Object
4: Object
$$added: function () { [native code] }
$$error: function () { [native code] }
$$moved: function () { [native code] }
$$removed: function () { [native code] }
$$updated: function () { [native code] }
$add: function () { [native code] }
$destroy: function () { [native code] }
$getRecord: function () { [native code] }
$indexFor: function () { [native code] }
$inst: function () { [native code] …Run Code Online (Sandbox Code Playgroud) 我想知道如何对项目应用部分更新$firebaseArray,文档很混乱,没有提到这一点..$save似乎破坏了整个项目:
var ref = new Firebase("https://xxxx.firebaseio.com/items");
$scope.items = $firebaseArray(ref);
someItem = getItem();
someItem.newField= 'Test';
$scope.items.$save(someItem);
Run Code Online (Sandbox Code Playgroud)
这会导致整个项目对象被完全覆盖,而不仅仅是更新 newField
我已经看过其他关于如何使用这个问题的讨论,$asObject但是我仍然很困惑如何处理这个问题$firebaseArray
我使用angularfire 1.0.0从我的firebase中获取一个唯一项目时遇到问题.为了澄清,我希望我的应用程序获取一个给定一个唯一的firebase id的帖子,例如"-JkZwz-tyYoRLoRqlI_I".它适用于在应用程序中导航,例如单击指向特定帖子的链接,但不会刷新.我的猜测是它与同步有关.现在它可以在获取所有帖子并在ng-repeat中使用它时起作用.这是导航到页面时为什么它适用于一个项目的线索.这应该不是很难,因为这应该是一个非常标准的操作,但我不能让它工作.我到处搜索,但实际上没有这方面的指南.在他们提到的API中$getRecord(key)
返回给定键的数组记录.如果未找到密钥,则返回null.此方法利用$ indexFor(key)查找相应的记录.
但这不符合预期.或者我错过了什么?
它适用于ng-repeat,如下所示:
<div ng-repeat="postt in posts">
<div>
<h1>{{postt.title}}</h1>
<div>{{postt.timestamp}}</div>
<div>{{postt.content}}</div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
但不是像这样的独特项目:
<div>
<h1>{{post.title}}</h1>
<div>{{post.timestamp}}</div>
<div>{{post.content}}</div>
</div>
Run Code Online (Sandbox Code Playgroud)
这是服务:
'use strict';
angular.module('app.module.blog.post')
.factory("PostService", ["$firebaseArray", "FIREBASE_URL", function($firebaseArray, FIREBASE_URL) {
var ref = new Firebase(FIREBASE_URL + "posts");
var posts = $firebaseArray(ref);
return {
all: posts, // ng-repeat on this works fine
last: function(nr) {
var query = ref.orderByChild("timestamp").limitToLast(nr);
return $firebaseArray(query); // ng-repeat on this work fine to
},
create: function (post) {
return posts.$add(post);
},
get: …Run Code Online (Sandbox Code Playgroud) 在这里整天用这个东西杀了我自己,我有两个部门和Articals:
export class Department {
articals?: Artical[]=[];
moms?: number;
id?: string;
constructor() {
}
}
Run Code Online (Sandbox Code Playgroud)
和:
export class Artical {
moms?: number;
price: number;
name?: string;
constructor() {
}
}
Run Code Online (Sandbox Code Playgroud)
喜欢你可以看到部门包含一个'Articals'数组,所以我正在用新的articals(及其属性)构建新的部门,并试图将它添加到fireStore:
this.departmetsCollection.doc(departmentId).set(Object.assign({},
myDepartment));
Run Code Online (Sandbox Code Playgroud)
但得到错误:"函数DocumentReference.set()调用无效数据.不支持的字段值:自定义Artical对象"
Ps尝试只将部分艺术品添加到部门的路径中
doc(departmentId).set({'articals':myArticals});
Run Code Online (Sandbox Code Playgroud)
并且使用'update'而不是'set'但也没有任何帮助:S将这种对象添加到firestore是正确的?
angularfire ×10
angularjs ×6
firebase ×6
angular ×4
arrays ×2
angularfire2 ×1
javascript ×1
karma-runner ×1
key ×1
timestamp ×1
typescript ×1
unit-testing ×1