如何確定.find退貨是否退貨?我正在努力做到這一點,所以如果它什么都不回傳,它會保存一個帶有身份驗證令牌的模式(檢查 2),如果它回傳一些東西,它會檢查 3。但每次,即使我的discordIdin沒有模式它記錄的資料庫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 ');
}
uj5u.com熱心網友回復:
您可以使用該length值來檢查find()結果的長度,例如:
const discordIdCheck = await authSchema.find({
discordId: discordId,
});
if(discordIdCheck.length <= 0) console.log("0 found")
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/408427.html
標籤:
