4

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

7
  • Which is these values is causing the error? Commented Jan 10, 2021 at 15:08
  • Badges - although any second value can be the error (for example if I erase badges, addons will be the problem) Commented Jan 10, 2021 at 19:57
  • And what is the complete error message, as usually it should tell you why it can't encode the value you pass in? Commented Jan 10, 2021 at 20:36
  • I have appended the error message to the question Commented Jan 10, 2021 at 21:09
  • The message you shared mentions "at Array.map", on firestore it's no possible to declare empty arrays. Try updating the set without the empty arrays and it should be able to encode the values Commented Jan 11, 2021 at 23:55

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.