我正在使用 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
我只尝试了 user 字段并且它可以工作,但是添加任何其他字段都会导致它执行相同的错误。我也尝试使用两种不同的 set
声明并将第一个字段与所有其他字段制作成 map ,但这些都不起作用。最佳答案
您分享的错误提到了“在 Array.map”,在 firestore 上不可能声明空数组。更新没有空数组的集合,它应该能够对值进行编码。
这与此 other question 中提到的类似我建议你看看 Firestore Best practices
关于javascript - Node.js Firestore 错误 - 无法编码值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65654037/