Ember Uncaught Error: Attempted to handle event `didSetProperty` - ember.js

I am getting this error and it seems to be when I destroy the session on my "auth" logic. I have read everything I can find and it seems to be something to do with the values of the object that is being destroyed, but I've tried everything I have found and no luck. If anyone could point me in the right direction to a solution I would greatly appreciate it.
Error
Uncaught Error: Attempted to handle event didSetProperty on while in state root.deleted.saved. Called with {name: email, oldValue: undefined, originalValue: undefined, value: test#test.com}.
Controller
App.ApplicationController = Ember.ObjectController.extend
actions:
signOut: ->
#get('model').destroyRecord().then =>
$.removeCookie 'apiKey'
#set 'model', #store.createRecord 'session'
#transitionToRoute 'sign-in'
, ->
#transitionToRoute 'sign-in'
Route
App.ApplicationRoute = Ember.Route.extend
model: ->
if !$.cookie 'apiKey'
#store.createRecord 'session'
else
#store.find('session', 'current').then (session)->
session
Session Model
App.Session = DS.Model.extend
access_token: DS.attr()
email: DS.attr()
password: DS.attr()
isLoggedIn: Ember.computed.notEmpty 'access_token'
user: DS.belongsTo 'user', embedded: 'always'
App.SessionSerializer = DS.ActiveModelSerializer.extend DS.EmbeddedRecordsMixin,
attrs: user: embedded: 'always'

This is a bit late, sorry.
When the promise to destroy the model is resolved, something is being set on the model - session record in this case. Please note the #get('model').destroyRecord() command is followed by the #set 'model' command.
That is why the error message has while in state root.deleted.saved.

Related

ember data 1.13.8 and ember cli mirage confusion

This is my Ember inspector info tab:
I am using ember-cli-mirage and I'm facing issue here.
I am not able to get belongsTo relationship of a model. When I do car.get('user'), I get a promise in result which gets fullfilled but the value is always null.
My user model
import DS from 'ember-data';
export default DS.Model.extend({
name: DS.attr('String'),
car: DS.belongsTo('car',{async: true})
});
My car model
import DS from 'ember-data';
export default DS.Model.extend({
color: DS.attr('String'),
user: DS.belongsTo('user',{async: true})
});
my mirage/config
this.get('/cars',function(db,request) {
var qp = request.queryParams.searchString.toLowerCase();
return {
cars: db.cars.where({'color':qp}),
users: db.users
};
});
I get the list of cars with search color but I don't get the user.
it returns a promise which when fullfilled gives null.
Since I am using ember-data 1.13.8
I tried using this Mirage working with json but then I get error
datum model is not defined error so i guess new json api is not my issue for some reason my ember data uses old json format and original config works.
this.get('/cars', function(db, request) {
return {
data: db.cars.map(attrs => (
{type: 'cars', id: attrs.id, attributes: attrs }
))
};
})
My cars route look like this
model: function(params) {
self = this;
if(!params){
return [];
}
return this.store.findQuery('car',params);
},
I tried
return this.store.findQuery('car',params).then(function(item){
console.log(item.get('user'));
});
but i still got console.log //undefined
I can see user_id in my json returned.
SOLUTION:
found the issue i was saving relationship as user_id = user.id. It should be user=user.id
SOLUTION: found the issue i was saving relationship as user_id = user.id. It should be user=user.id

EmberJS; Can't create record with relationships

I've a got User, Chat and Message model to create chat functionality in our app.
User:
messages: DS.hasMany 'message'
chat: DS.hasMany 'chat'
Chat:
users: DS.hasMany 'user'
messages: DS.hasMany 'message'
Message:
user: DS.belongsTo 'user'
chat: DS.belongsTo 'chat'
message: DS.attr 'string'
In this situation I already have a chat and a user object and what I'm trying to do is create a message in this chat which belongs to the user sending it.
I'm using the following to do so:
controller = Ember.ObjectController.extend Ember.Evented,
needs: 'application'
app: Ember.computed.alias 'controllers.application'
actions:
sendMessage: ->
content = Ember.$('.new-message input').val()
message = #store.createRecord 'message', message: content
message.set 'user', #get 'app.model.user'
message.set 'chat', #get 'model'
message.save().then =>
console.log "done"
But it results in the following error:
Uncaught Error: Assertion Failed: Cannot call get with 'isEmpty' on an undefined object
Two things that make the problem disappear:
Removing the message.set 'chat', #get 'model' line
Creating a message in the database (it works if it's not the first message being created)

How to properly get hasMany model in route

I have 2 models Post and Comment. Both have own routes: PostRoute and PostCommentsRoute, in the first case I simply use #store.find to load model data but I have a problem with the second route. When I try to refer to Post and then get its comments, they loads but don't update when I add new one. If I use #store.find 'comment', it stays updated however loads everything, so it's not the point. How to get only comments related to the post and keep them updated?
App.Post = DS.Model.extend
comments: DS.hasMany 'comment', async: true
App.Comment = DS.Model.extend
post: DS.belongsTo 'post'
text: DS.attr 'string'
App.Router.map ()->
#resource 'post', path: '/post/:post_id', ()->
#route 'comments'
App.PostCommentsController = Em.ArrayController.extend
newComment: ""
needs: 'post'
post: Em.computed.alias 'controllers.post'
sortProperties: ['created'],
sortAscending: false
actions:
addComment: ()->
commentText = #get 'newComment'
post = #get 'post'
comment = #store.createRecord 'comment',
text: commentText
post: post.get 'content'
#set 'newComment', ''
comment.save()
App.PostRoute = Em.Route.extend
model: (params)->
#store.find 'post', params.post_id
App.PostCommentsRoute = Em.Route.extend
model: (params)->
# loads only comments to parent post but doesn't update
post = #modelFor 'post'
post.get 'comments' # /api/posts/1/comments
App.PostCommentsRoute = Em.Route.extend
model: (params)->
# updates but loads all comments
#store.find 'comment' # /api/comments
After you save your comment you have to push it on the post's comments:
comment.save().then(function () {
post.get('comments').push(comment);
// or if that doesn't work you may need to resolve the promise:
post.get('comments').then(function (comments) {
comments.push(comment);
});
});

Ember DS.Store error while submitting form

I'm a newbie to ember and I'm trying to create a basic sign-up form.
Relevant model:
App.NewUser = DS.Model.extend({
user_name: DS.attr('string'),
password: DS.attr('string'),
confirm_password: DS.attr('string'),
email: DS.attr('string'),
first_name: DS.attr('string'),
last_name: DS.attr('string'),
});
Relevant controller:
App.SignupController = Ember.ArrayController.extend({
actions: {
signup: function() {
var data = this.getProperties('first_name', 'last_name', 'email', 'user_name', 'password', 'confirm_password');
var newUser = this.store.createRecord('newUser', data);
newUser.save();
},
},
});
When the "signup" action executes, I get the following error:
Error: Attempted to handle event `didSetProperty` on <App.NewUser:ember332:null> while in state root.deleted.saved. Called with {name: last_name, oldValue: undefined, originalValue: undefined, value: undefined}.
What am I doing wrong?
This is a bug, Ember Data is setting the record state incorrectly if you're setting a value to what it's currently set to (undefined on createRecord)
You'll want to either coerce your values into empty strings or not set undefined values while creating the record.
for(var key in data){
if(!data[key]) delete data[key];
}
http://emberjs.jsbin.com/OxIDiVU/124/edit
https://github.com/emberjs/data/issues/1648

Accessing relations from templates with ember-data 1.0.0 beta

App.User = DS.Model.extend({
posts: DS.hasMany('post', {async: true})
});
App.Post = DS.Model.extend({
body: DS.attr(),
user: DS.belongsTo('user')
});
App.ProfileRoute = Ember.Route.extend({
model: function(params) {
return this.get('store').find('user', params.user_id)
}
});
and in template
{{#each post in model.posts}}
{{post.body}}
{{/each}}
json for user. I don't want embed posts in user json
{user: { posts: [1, 2, 3] }}
This don't render anything. It receives posts json from server after this error occur
Assertion failed: You looked up the 'posts' relationship on '' but some of the associated records were not loaded. Either make sure they are all loaded together with the parent record, or specify that the relationship is async (DS.attr({ async: true }))
In chrome inspector I see all data loaded properly.
How can I solve this? Should I preload all models I want to use in templates?
The model function in your route is missing the return, so the error is being thrown when you try to access model.posts because there is no model.
App.ProfileRoute = Ember.Route.extend({
model: function(params) {
return this.get('store').find('user', params.user_id);
}
});
Have you tried to just write {{#each posts}}?
Worked for my project.
Then write {{body}} within the each block.
Let me know, thanks!