【问题标题】:Loopback + Angular: `findById` Error. Expected response to contain an object but got an arrayLoopback + Angular:`findById` 错误。包含对象但得到数组的预期响应
【发布时间】:2017-03-10 05:48:23
【问题描述】:

我正在尝试为聊天室应用过滤器,以便我只看到与显示的聊天室具有外键关系的消息,因此我尝试将 shownMessages 传递给视图。我如何有效地做到这一点?我正在处理的当前错误是Error: [$resource:badcfg] Error in resource configuration for actionfindById. 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 是我在 chat-room.json 中生成的 ChatRoom 和 ChatMessage 之间的环回中的外键关系的名称:

{
  "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 但不确定如何

【问题讨论】:

  • 在 find by id 方法中,可能是 id 参数获取了其他值而不是数字,因此请在控制台中检查 $stateParam 返回的内容
  • 好的,谢谢,我越来越不确定了
  • 我现在只是不确定如何通过外键获取属于聊天的所有消息。我正在尝试使用 stateparams 但不确定如何
  • 我可能需要某种 where 子句

标签: angularjs filter loopback


【解决方案1】:

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

【讨论】:

  • 感谢它最终未定义。我只是不确定此时如何通过外键获取属于聊天的所有消息。我正在尝试使用 stateparams 但不确定如何
【解决方案2】:

我得到了我需要的东西。需要一个包含过滤器! 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);
  }])

【讨论】:

    猜你喜欢
    • 2014-05-14
    • 2015-03-11
    • 2014-08-16
    • 2013-12-01
    • 2013-12-17
    • 1970-01-01
    • 2015-08-20
    • 1970-01-01
    • 2016-09-27
    相关资源
    最近更新 更多