編輯:我已經解決了我的問題。解決方案在評論中。
我正在嘗試允許用戶將其他帳戶鏈接到他們在我的應用程式上的主要帳戶。當他們去鏈接一個帳戶時,我想確保他們嘗試鏈接的帳戶尚未被其他用戶鏈接。
我似乎無法獲得查詢權限來搜索我的資料庫以查找鏈接帳戶的任何用戶。我已經成功實作了創建新關聯帳戶的功能,只需要完成確保它尚未被占用。
我當前的查詢如下(其中 platformID 和 platform 在請求中正確傳遞并且已經存在于linkedAccounts陣列中的資料庫中):
const profileExists = await User.findOne({linkedAccounts: {platformUserIdentifier: platformID, platform: platform}})
用戶方案:
const userSchema = mongoose.Schema(
{
username: {
type: String,
required: true,
},
email: {
type: String,
required: true,
unique: true,
},
password: {
type: String,
required: true,
},
pic: {
type: String,
required: true,
default:
"https://icon-library.com/images/anonymous-avatar-icon/anonymous-avatar-icon-25.jpg",
},
linkedAccounts: [
{
platformUserIdentifier: String,
platform: String
}
],
},
{
timestamps: true,
}
);
我的查詢對我來說是正確的,但繼續回傳null。我已經仔細檢查以確保它與請求中的變數無關,并且我還附上了我的資料庫的螢屏截圖以證明我正在搜索的內容存在。
MongoDB 影像
我遇到了障礙,非常感謝任何人的幫助。
提前致謝。
uj5u.com熱心網友回復:
您可以使用 mongodb 做到這一點,但在 mongoose 中,您必須找到陣列物件中的每個鍵
const profileExists = await User.findOne({
"linkedAccounts.platformUserIdentifier" : platformID,
"linkedAccounts.platform" : platform
})
mongodb 的游樂場https://mongoplayground.net/p/NPGcUJl5MLw
uj5u.com熱心網友回復:
我需要用來查找帳戶的代碼如下:
const profileExists = await User.findOne({
linkedAccounts: {$elemMatch: {platformUserIdentifier: platformID, platform: platform}}
})
這將回傳具有上述內容的User組態檔platformIDplatformlinkedAccounts
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/480727.html
