question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

The id xxx has already been used with another record of type

See original GitHub issue

I’m attempting to check if a user model exists before creating it but I’m getting: Error: Assertion Failed: The id xxx has already been used with another record of type app@model:user.

This is my code:

// authData comes from Firebase
checkUser: function() {
   var savedUser = this.get('store').find('user', this.get('authData.uid'));
   savedUser.then(function(savedUser) {
      self.set('user', savedUser);
   }, function(user) {
      self.createUser();
   });
},

createUser: function(user) {
   var self = this;

  // it fails here!

   var newUser = this.get('store').createRecord('user', {
      id: this.get('authData.uid'),
      created: new Date().getTime()
   }).save().then(function(newUser) {
      Ember.debug('created a new user');
      self.set('user', newUser);
   }, function() {
      Ember.debug('could not save user');
      self.set('user', null);
   });
}

If I leave the check out, the code runs but overwrites the user model on every login, which I don’t believe is desirable. I’m using Ember 1.8.0-beta.4 and Emberfire 1.2.7.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
tstirratcommented, Apr 3, 2015

This does not appear to be a bug in emberfire, rather that ember-data creates a temporary record in its store when you do find (regardless of whether it is found or not):

var user = store.find('user', 'simplelogin:1'); // does not exist on server
store.recordForId('user', 'simplelogin:1'); // exists!

here’s my workaround:

var id = 'simplelogin:1';
store.find('user', id)
  .then(function(u) {
    console.log('found user with id', id);
    return u;
  })
  .catch(function () {
    console.log('can create with id', id);
    store.recordForId('user', id).unloadRecord(); // remove the unresolved record
    return store.createRecord('user', {
      id: id,
      created: +new Date()
    });
  })
  .then(function (u) {
   // u = found or created user
   console.log('found or created', u);
  });
1reaction
oskarroughcommented, Oct 23, 2014

Hey @20v100, there are two parts: checking if a record exists and if not, create it. So I ended up replacing the check with native Firebase code instead of using the Emberfire adapter. It’s a temporary solution that works for me so far .

// Check if the user exists using native Firebase because of a emberfire problem with "id already in use"
ref.child('users').child(userId).once('value', function(snapshot) {
   var exists = (snapshot.val() !== null);
   userExistsCallback(userId, exists);
});

// Do the right thing depending on whether the user exists
function userExistsCallback(userId, exists) {
   Ember.debug('user exists? ' + exists);
   if (exists) {
      // do something
   } else {
      // do something
   }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

The id xxx has already been used with another record of type
I'm attempting to check if a user model exists before creating it but I'm getting: Error: Assertion Failed: The id xxx has already...
Read more >
Troubleshooting submissions - Crossref
Solution 1: Change the journal level DOI in your submission to match the previously registered journal level DOI and resubmit.
Read more >
Working with Authority Records - Ex Libris Knowledge Center
To create a local authority record: Open the MD Editor. Select File > New and select from the list of local vocabularies that...
Read more >
Supported DNS record types - Amazon Route 53
Describes the DNS record types that are supported by Route 53. ... You can't create a CAA record and a CNAME record that...
Read more >
Claim Denial Codes List - Utah Medicaid
Procedure is sex specific. Client is the wrong sex for procedure. Verify ID#. 3. The procedure code is inconsistent with the patient's gender....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found