【问题标题】:Typescript error - Expected 1 arguments, but got 0打字稿错误 - 预期 1 个参数,但得到 0
【发布时间】:2018-11-18 01:09:11
【问题描述】:

我在 Ionic/Angular 中的打字稿代码出现错误。错误输出为:

[22:55:09] 打字稿:C:/xampp/htdocs/project x/anonymous-social/src/pages/chat/chat.ts,行:103 预期有 1 个参数,但得到了 0 个。

L102: if(!this.isUserThreadEmpty) {

L103: let threadKey = this.database.list('users/'+this.userData.uid+'/threads/'+this.recipient).push().key;

L104: 让收件人数据 = {

我不知道为什么会这样,我猜push() 需要一个参数,但是我在网上看到的关于如何在 firebase 中获得key 的所有stackoverflow 答案都指向了这个解决方案,所以我不是确定发生了什么。

实际代码为:

if(!this.isUserThreadEmpty) {
  let threadKey = this.database.list('users/'+this.userData.uid+'/threads/'+this.recipient).push().key;
  let recipientData = {
    recipient: this.recipient,
    threadId: threadKey,
    displayName: this.displayName,
  }

有什么想法吗?我需要将key 传递给recipientData...我做错了什么?

【问题讨论】:

  • I would guess that push() needs an argument - 是的,确实如此...all the stackoverflow answers I saw online ...你能链接到在这样的数组上使用.push()的那个吗?
  • @JaromandaX 这个例如:stackoverflow.com/questions/38768576/…
  • @JaromandaX 官方文档:firebase.google.com/docs/database/admin/…
  • 哦,所以 this.database.list('users/'+this.userData.uid+'/threads/'+this.recipient) 不是数组 - 抱歉,我的错

标签: typescript firebase firebase-realtime-database angularfire2


【解决方案1】:

您正在使用 AngularFire2。 push method in AngularFire2 需要一个参数。

您链接的问题和文档适用于常规 Firebase JavaScript SDK,其中 push 的参数是可选的。

您最好的选择是使用常规的 JavaScript SDK 执行此操作,因为通过 AngularFire2 包装器执行此操作没有优势:

let threadKey = firebase.database().ref().push().key;

来自 Jane 的评论:要使用 Firebase JavaScript SDK,您需要:

import * as firebase from 'firebase/app';

更新 (2018-06-08):我刚刚发现还有一个专用方法 AngularFireDatabase.createPushId() 可以创建推送 ID。所以我认为在你的情况下是database.createPushId()

【讨论】:

  • 有趣,好的。如何加载 firebase 变量?我得到Cannot find name 'firebase'. 我目前只在我的.ts 文件中加载AngularFireDatabase...
  • 为了将来在 AngularFire2 上的参考需要导入:import * as firebase from 'firebase/app';,然后您可以简单地使用firebase.database().ref().push().key; 来生成密钥。
猜你喜欢
  • 2021-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-29
  • 2021-07-29
  • 2021-08-22
  • 2019-08-28
  • 1970-01-01
相关资源
最近更新 更多