Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js Firestore Error - Cannot encode value

I'm using Discord.js to make a Discord bot, and I'm trying to use Cloud Firestore to keep a database of my values. I have the collection users in the firestore database. Here is what I have:

//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);
    });

The console outputs creating... but then it outputs the error Cannot encode value I tried with only the user field and it works, but adding any other field will cause it to do that same error. I also tried using two different set statements and making the first field a map with all the other fields, but neither of those worked.

Full error message

like image 768
Super Phantom User Avatar asked Sep 17 '25 12:09

Super Phantom User


1 Answers

The error you shared mentions "at Array.map", on firestore it's no possible to declare empty arrays. Update the set without the empty arrays and it should be able to encode the values.

This is similar to what is mentioned on this other question and I would recommend you take a look at Firestore Best practices

like image 118
Louis C Avatar answered Sep 19 '25 01:09

Louis C