Loopback + Angular:`findById`错误。包含对象但得到数组的预期响应

时间:2017-03-09 19:06:11

标签: angularjs filter loopback

我试图为聊天室应用过滤器,以便我只看到与该聊天室显示外键关系的消息,所以我试图通过{{1到视图。我该如何有效地做到这一点?我处理的当前错误是shownMessages findById Error: [$resource:badcfg] Error in resource configuration for action。我尽可能地搜索,但仍然无济于事。

. Expected response to contain an object but got an array

// 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生成的ChatRoom和ChatMessage之间的回送中建立的foriegn密钥关系的名称:

chat-room.json

编辑:如何通过外键获取属于聊天的所有邮件?我试图使用stateparams但不确定如何

2 个答案:

答案 0 :(得分:1)

尝试打印$stateParams.messagesInChat值。 如错误所示,它包含一个数组而不是一个对象(表示存在多个值而不是单个ID),但findByID只接受一个值,因为您只查找一个ID的数据。

答案 1 :(得分: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);
  }])