【问题标题】:Node.js Firestore Error - Cannot encode valueNode.js Firestore 错误 - 无法编码值
【发布时间】:2021-01-10 13:54:47
【问题描述】:

我正在使用 Discord.js 制作 Discord 机器人,并且我正在尝试使用 Cloud Firestore 来保存我的价值观的数据库。我在 firestore 数据库中有集合用户。这是我所拥有的:

//TOP OF CODE

let admin = require('firebase-admin');
let serviceAccount = require("./coinflipper-18c5c-firebase-adminsdk-cb16g-458fcb58c8.json");

admin.initializeApp({
  credential: admin.credential.cert(serviceAccount)
});

let firestore = admin.firestore();

//IN BOT.ON("MESSAGE") EVENT
//also by the way I use msg instead of message

let userData = firestore.doc(`users/${msg.author.id}`);
  if (!userData.exists) {
    console.log("creating...");
    userData.set({
        user: msg.author.id,
        badges: [],
        addons: {
          addonInv: [],
          buyNotification: false,
          customAddons: []
        },
        banned: false,
        cooldowns: {
          daily: 0,
          dropshipCooldown: false,
          exploreCooldown: false,
          flipCooldown: false,
          lotteryCooldown: false,
          monthly: 0,
          work: 0
        },
        currencies: {
          cents: 0,
          flipped: 0,
          minigames_won: 0,
          onto: 0,
          register: 0,
          timesWorked: 0
        },
        dropshipping: {
          list: [],
          priceList: [],
          helpMessageId: 0
        },
        inventory: [],
        job: "none",
        karate: {
          abilities: [],
          against: 0,
          askedBy: 0,
          askedTo: 0,
          belt: "NA",
          channel: 0,
          choosing: false,
          chosen_abilities: [],
          first: false,
          guild: 0,
          hp: 0,
          in_battle: false,
          level: 0,
          mhp: 0,
          mst: 0,
          name: "NA",
          st: 0,
          timesWon: 0,
          turn: false,
          type: "NA",
          xp: 0
        },
        lottery: {
          id: 0,
          prize: 0,
          won: false
        }
    }).catch(error => {
      console.error(error);
    });

控制台输出creating...,但随后输出错误Cannot encode value 我只尝试了用户字段并且它可以工作,但是添加任何其他字段都会导致它出现同样的错误。我还尝试使用两个不同的 set 语句,并将第一个字段与所有其他字段制作成地图,但这些都不起作用。

【问题讨论】:

  • 这些值是什么导致了错误?
  • 徽章 - 尽管任何第二个值都可能是错误(例如,如果我删除徽章,插件将是问题)
  • 完整的错误信息是什么,通常它应该告诉你为什么它不能对你传入的值进行编码?
  • 我已将错误消息附加到问题中
  • 您分享的消息提到“在 Array.map”,在 Firestore 上无法声明空数组。尝试更新没有空数组的集合,它应该能够对值进行编码

标签: javascript node.js firebase google-cloud-firestore


【解决方案1】:

您分享的错误提到“在 Array.map”,在 Firestore 上无法声明空数组。更新没有空数组的集合,它应该能够对值进行编码。

这类似于other question 中提到的内容,我建议您查看Firestore Best practices

【讨论】:

    猜你喜欢
    • 2018-06-05
    • 2020-11-14
    • 1970-01-01
    • 2014-03-04
    • 2018-06-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-30
    • 2014-12-26
    相关资源
    最近更新 更多