Skip to content
This repository

cover null case for belongsTo relationship #564

Merged
merged 1 commit into from over 1 year ago

6 participants

Jeff LaBarge Pepe Cano Benjamin Rhodes Andy Leeper Gabe Smedresman Peter Wagenet
Jeff LaBarge

I was getting an error:

TypeError: Cannot read property 'clientId' of null

The error was caused by the fact that this statement doesn't protect against a null id:

if (typeof id === 'object') {
  return store.findByClientId(type, id.clientId);
}

This pull request adds the null check, and also a test.

Pepe Cano

Fixed the same issue for me, thanks.

Benjamin Rhodes

Ran into this too. This commit fixes the issue.

Andy Leeper

:+1: to the problem and solution.. thanks @jefflab

Gabe Smedresman

+1

Jeff LaBarge

Thanks to everyone for letting me know this worked for you.

This is my first contribution to ember, so please let me know if there is anything else I need to do before the framework team accepts this pull request.

Peter Wagenet
Owner

@sandstrom I think the issue here is that typeof null === 'object'. So the old check was letting null through.

Peter Wagenet wagenet merged commit 2ee743b into from January 08, 2013
Peter Wagenet wagenet closed this January 08, 2013
Jeff LaBarge jefflab deleted the branch January 10, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Dec 30, 2012
Jeff LaBarge cover null case for belongsTo relationship a6bc1c1
This page is out of date. Refresh to see the latest.
7  packages/ember-data/lib/system/relationships/belongs_to.js
@@ -22,10 +22,13 @@ DS.belongsTo = function(type, options) {
22 22
 
23 23
     id = data[key];
24 24
 
25  
-    if (typeof id === 'object') {
  25
+    if(!id) {
  26
+      return null;
  27
+    }
  28
+    else if (typeof id === 'object') {
26 29
       return store.findByClientId(type, id.clientId);
27 30
     } else {
28  
-      return id ? store.find(type, id) : null;
  31
+      return store.find(type, id);
29 32
     }
30 33
   }).property('data').meta(meta);
31 34
 };
6  packages/ember-data/tests/integration/relationships/one_to_many_relationships_test.js
@@ -42,6 +42,12 @@ function verifySynchronizedOneToMany(post, comment, expectedHasMany) {
42 42
   deepEqual(post.get('comments').toArray(), expectedHasMany);
43 43
 }
44 44
 
  45
+test("Referencing a null belongsTo relationship returns null", function(){
  46
+  store.load(App.Comment, { id: 1, post: null, body: "child with intentionally null parent" });
  47
+  var comment = store.find(App.Comment, 1);
  48
+  equal(comment.get('post'), null, "null belongsTo relationship returns null");
  49
+});
  50
+
45 51
 test("When setting a record's belongsTo relationship to another record, that record should be added to the inverse hasMany array", function() {
46 52
   store.load(App.Post, { id: 1, title: "parent" });
47 53
   store.load(App.Comment, { id: 2, body: "child" });
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.