2017-03-09 40 views
0

我想爲一個聊天室應用一個過濾器,使得我只看到與該聊天室顯示的外鍵關係的消息,所以我試圖將shownMessages傳遞給視圖。我如何有效地做到這一點?我正在處理的當前錯誤是Error: [$resource:badcfg] Error in resource configuration for action findById . Expected response to contain an object but got an array。我儘可能地搜索,儘管如此,沒有任何可用的。Loopback + Angular:findById錯誤。預期的響應包含一個對象,但得到了一個數組

// for inside the room 
    // node - injection order is extremely important 
    .controller('InsideRoomController', ['$scope', '$q', 'ChatRoom', 'ChatMessage', '$stateParams', '$state', function($scope, $q, 
     ChatRoom, ChatMessage, $stateParams, $state) { 
     // we include Chatroom as a param to the controller and func since we work with that to display it's contents 
     // only show messages pertaining to that room 
     $scope.shownMessages = []; 

     ChatMessage 
      .findById({ id: $stateParams.messagesInChat }) 
      .$promise 
      .then(function(showMessages) { // once we query to find chat rooms 
      $scope.shownMessages = shownMessages; 
      }); 

    }]) 

relationsInChat是我在回送聊天室和ChatMessage之間作出foriegn鍵關係這是在chat-room.json產生的名稱:

{ 
    "name": "ChatRoom", 
    "base": "PersistedModel", 
    "idInjection": true, 
    "options": { 
    "validateUpsert": true 
    }, 
    "properties": { 
    "name": { 
     "type": "string", 
     "required": true 
    }, 
    "city": { 
     "type": "string" 
    } 
    }, 
    "validations": [], 
    "relations": { 
    "ChatMessagers": { 
     "type": "hasMany", 
     "model": "ChatMessager", 
     "foreignKey": "" 
    }, 
    "chatMessages": { 
     "type": "hasMany", 
     "model": "ChatMessage", 
     "foreignKey": "messagesInChat" 
    } 
    }, 
    "acls": [], 
    "methods": {} 
} 

編輯:我如何獲得屬於聊天的所有消息通過外鍵?我試圖使用stateparams,但不知道如何

+0

通過ID方法的發現,可能是** ** ID PARAM得到一些其他的值,而不是數量,所以在控制檯什麼$ stateParam正在恢復 –

+0

好感謝檢查,我越來越不確定 –

+0

我米只是不知道如何獲得屬於通過外鍵聊天的所有消息在這一點..我試圖使用stateparams,但不知道如何 –

回答

1

嘗試打印$stateParams.messagesInChat值。 正如錯誤所示,它包含一個數組而不是對象(意味着存在多個值而不是單個ID),但findByID只接受一個值,因爲您只爲一個ID查找數據。

+0

感謝它結束了未定義。我只是不知道如何通過外鍵獲取屬於聊天的所有消息。我試圖使用stateparams,但不知道如何 –

0

我得到了我需要的東西。需要一個包含過濾器! http://loopback.io/doc/en/lb2/Include-filter.html

.controller('InsideRoomController', ['$scope', '$q', 'ChatRoom', 'ChatMessage', '$stateParams', '$state', function($scope, $q, 
     ChatRoom, ChatMessage, $stateParams, $state) { 
     // we include Chatroom as a param to the controller and func since we work with that to display it's contents 
     // only show messages pertaining to that room 
     $scope.shownMessages = []; 

     function getMsgs() { 
      //User.find({where: {vip: true}, limit: 10}, cb); 
      return (ChatRoom.find({include: ['ChatMessages']})) 
     }; 

     $scope.shownMessages = getMsgs(); 
     console.log($scope.shownMessages); 
    }]) 
相關問題