【发布时间】:2022-01-08 21:23:27
【问题描述】:
如何确定.find 是否返回某些内容?我正在努力做到这一点,所以如果它什么都不返回,它会保存一个带有身份验证令牌的模式(检查 2),如果它返回一些东西,它会检查 3。但每次,即使我的 @ 没有模式987654322@ 在它记录check 3 和check 1 但从不记录check 2 的数据库中。
let discordId = message.author.id;
const discordIdCheck = authSchema.find({
discordId: discordId,
});
console.log('check 1');
if (discordIdCheck.count() < 1) {
function getAuthToken() {
var letters =
'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!.#&';
var authTokenStart = '';
for (var i = 0; i < 12; i++) {
authTokenStart += letters[Math.floor(Math.random() * letters.length)];
}
return authTokenStart;
}
const authToken = getAuthToken();
const auth = {
discordId: discordId,
authToken: authToken,
};
console.log('check 2');
message.author.send('Okay, your auth token is: ' + authToken);
console.log(authToken);
new authSchema(auth).save();
} else {
message.channel.send(
'You already have an auth token. use !token to make me send it to you in your direct messages',
);
console.log('Check 3 ');
}
【问题讨论】:
-
你是不是直接查数据库验证
authSchema是空的? -
直接检查套件是什么意思? authSchema 等于 require('auth-schema.js') 如果你想要的话
-
@BuzzMoschetti,回复中没有提到你*
-
你的逻辑看起来不错;第一原则表明
discordId确实存在,这就是为什么您不断收到Check 3。 -
find和count不是 Mongoose 中的两个异步函数吗?
标签: javascript node.js mongodb mongoose discord.js