私はモンゴ/マングースに新たなんだと私は次のことを実行するためにどのように問題を抱えています:削除/追加要素に応じて
私はそれがナイトクラブ/バーを見つけるために、YelpのAPIを利用して簡単なアプリを作成していますユーザーが検索できる地域の周りに
ユーザーにはクラブのリストが表示され、各リストには、特定のクラブに予約されている他のユーザーを追跡できるmongodbに送信されるフォームがあります。
私はclubIDはクラブのIDとゲストは、単にユーザ名を追跡します文字列の配列であるClubs
const clubSchema = new Schema({
clubID: String,
guests: [String]
})
のスキーマを作りました。場合
1)特定のclubID
がデータベースに存在しない場合、それはguests
2でuserName
を新しいものを作成して挿入されます):
は、私は次のようなことをしたいですclubID
が存在し、userName
は(それが別のユーザーのことを意味する)guests
に存在しないclubID
が存在し場合には、guests
アレイ
3)にuserName
をプッシュします
exports.UpdateGuestList = function(req, res, next){
const clubID = req.params.clubID;
const userName = req.params.userName
const userEmail = req.params.userEmail;
Club.findOne({ clubID: clubID}, function(err, existingClub){
if (err) { return next(err) ;}
// if clubID exist in the data base, check the following
if (existingClub){
//1) if the current userName exist, remove it from guests array
if (existingClub.guests.includes(userName)){
console.log('Remove guest')
} else{ //2) if the current userName doesnt exist, push it into guests aaray
console.log('Push in gueest')
}
return res.send({"message": "done"})
}
// If clubID does not exist, create and save clubID
const club = new Club({
clubID: clubID,
guests: [userName]
})
club.save(function(err){
if (err) {return next(err)}
res.send({"message": "done"})
})
})
}
何が欲しいですか? – Codesingh
基本的に1)2)と3) – Alejandro