2017-04-12 8 views
0

배열 푸시() 함수를 사용하여 기존 객체에 배열 (called: "wishlistArray")을 성공적으로 삽입하는 코드를 작성했습니다.배열 splice() 함수를 사용하여 객체의 하위 배열을 제거하려면 어떻게해야합니까?

이미 추측 할 수 있듯이 사용자가 위시리스트에 항목을 추가하려고 할 때 트리거됩니다. "udEnfEmy5DSBvDsSy" 삽입 방지하기 위해 그/그녀를 "위시리스트"

_id: "DFhcAYAtMBviczogo" 
createdDate: "04/12/2017" 
expiryDate: "04/22/2017" 
noOfViews: 125 

    wishListArray: Array[1] 
    0: Object 
     wishedBy: "udEnfEmy5DSBvDsSy" 

에 개체를 추가 이미했습니다

사용자 ID와 사용자가 있음을 나타냅니다, 선명도 내 개체 아키텍처를 보자

var selectedItemIDSet = Session.get('selectedItemIDSet'); 
var addToWishList = buyList.find(selectedItemIDSet); 
var currentUser = [Meteor.user()._id ]; 

var wheatherWishListedOrNot = buyList.find({ _id:selectedID, 'wishListArray.wishedBy': {$in: currentUser } }).count(); 

addToWishList.forEach(function(itemName){ 

var currentObjectID = itemName._id; 

     //### If wishListArrayArray array already exists in then... 
     if (itemName.wishListArray) { 

       var wishListArray = [,...itemName.wishListArray]; 

        //### If current user hasnt wish listed item then add to Wish List (wishListArray) array 
       if (wheatherWishListedOrNot == "0") { 

         wishListArray.push({ wishedBy: Meteor.user()._id }); 
         Session.set('wishListArrayToGo',wishListArray); 

         } 
       else if(wheatherWishListedOrNot == "1") { 
         //### Else if WishedListed by current user, Do absolutely nothing! 

         } 
       }         

     var wishListArray = Session.get('wishListArrayToGo'); 

     buyList.update(currentObjectID, {$set: {wishListArray: wishListArray} }); 

    });  
,617,451 : 현재의 사용자 ID가 이미 wishlistArray 배열에 존재하는 경우 wishlistArray 배열 중복, I는 다음의 코드에 따라, 확인

어떻게이 문제를 해결했는지 설명 했으므로 그 반대의 문제가 있습니까? wishListArray 배열에서 사용자를 제거하려면 어떻게해야합니까?

나는 아래 코드를 아무런 성공없이 시도했다.

var selectedItemIDSet = Session.get('selectedItemIDSet'); 
var removeFromWishList = buyList.find(selectedItemIDSet); 

var currentUser = [Meteor.user()._id ]; 

wheatherWishListedOrNot = buyList.find({ _id:selectedID, 'wishListArray.wishedBy': {$in: currentUser } }).count(); 


removeFromWishList.forEach(function(itemName){ 

var currentObjectID = itemName._id; 

     //### If wishListArrayArray array already exists in then... 
     if (itemName.wishListArray) { 

       var wishListArray = [,...itemName.wishListArray]; 

        //### If current user current user is on **WishedListArray**, then remove from **WishedListArray** array. 
       if (wheatherWishListedOrNot == "1") { 

         wishListArray.splice({ wishedBy: Meteor.user()._id }); 
         Session.set('wishListArrayToGo',wishListArray); 

         } 
       else if(wheatherWishListedOrNot == "0") { 
         //### Else if current user isnt on WishedListArray, Do absolutely nothing! 

         } 
       }         

     var wishListArray = Session.get('wishListArrayToGo'); 

     buyList.update(currentObjectID, {$set: {wishListArray: wishListArray} }); 

    });  

내가 제거하려고 코드는 나에게 논리적 보이지만 WishlistArray에서 사용자 ID를 제거하지 못합니다. 누군가 내가 잘못 가고있는 것을 친절하게 지적 할 수 있습니까?

미리 감사드립니다.

답변

2

간단한 몽고 연산자를 사용하여 원하는 것을 얻을 수 있습니다. 더 단순하게하기 위해 문자열 배열을 wishList (userIds)으로 사용할 수 있습니다.

는 사용자를 삽입하려면 :

buyList.update(currentObjectID, { $addToSet: { wishList: userId} }); 

addToSet은 단지 거기에 userId하지 않은 경우이를 삽입합니다.

다시 제거 :

buyList.update(currentObjectID, { $pull: { whishList: userId } }); 

어때?

+0

더 쉬운 방법! –

+0

@tomsp 감사합니다. buyList.update (currentObjectID, {$ pull : {wishListArray : userId}});를 실행 해 보았습니다. 하지만 아무것도 삭제 된 것 같습니다 ... 내가 뭘 잘못하고있어? – SirBT

+0

@tomsp 올바른 방향으로 나를 가리켜 주셔서 감사합니다. 약간의 연구에 따르면 내 객체 아키텍처에서 적절한 쿼리는 다음과 같습니다. buyList.update ({_ id : currentObjectID}, {$ pull : {wishListArray : {wished : userID}}}); – SirBT