2017-03-09 47 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); 
    }]) 
相关问题