Reputation: 496
I'm trying to apply a filter for a chat room that makes it so that I only see messages that have a foreign key relationship to that chat room displayed, so I'm trying to pass shownMessages
to the view. How do I do this effectively? The current error I'm dealing with is Error: [$resource:badcfg] Error in resource configuration for action
findById. Expected response to contain an object but got an array
. I'm searching as best as I can and still, nothing to my avail.
// 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
is the name of the foriegn key relation I made in loopback between ChatRoom and ChatMessage which was generated in 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": {}
}
Edit: How do I get all messages belonging to the chat via foreign key? I'm trying to use stateparams but not sure how
Upvotes: 0
Views: 175
Reputation: 496
I got what I needed. Needed an include filter! 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);
}])
Upvotes: 0
Reputation: 156
Try printing $stateParams.messagesInChat
value.
As the error shows, it contains an array rather than object(means multiple value is present instead of single ID), but findByID accept only one value as you are finding the data for only one ID.
Upvotes: 1