0

Im trying to create data if a facebook id exists in the database else create new data, however the first condition works fine but when its creating data nothing happens, ony get connot read property 'provider' of null even i hard code a provider value.

db.collection('accounts', function(err, collection){
    collection.findOne({'email': fbEmail}, function(err, item){
        var providerId = item.provider.facebook.id;
        console.log("facebook id should be: "+providerId);
        if(providerId == ""){
            collection.update({email:fbEmail, 'provider.facebook.id':""}, {$set: {"provider.facebook.id": fbId}}, {safe:true}, function(err){
                if(err){
                    console.log(err);
                }else{
                    console.log("id updated!");
                };
            });
        }else{ //creates a new user
            console.log("creating a new account...");
            collection.update({'email': fbEmail}, {
                'email': fbEmail,
                'provider':
                    {
                        'facebook': {'id': fbId},                            
                        'twitter': {'id': ""}                                
                    }                    
            }, {upsert:true}, function(err, doc){
                if(err){
                    console.log("Error finding email "+err);
                }                
            });             
        }
    });
});
nihulus
  • 1,475
  • 4
  • 24
  • 46
  • 1
    In your `findOne` callback you need to check if `item` is `null` or not before checking `item.provider`. `item` will be `null` if this is a new email. – JohnnyHK Jan 23 '13 at 17:29

1 Answers1

1

One (the first one) of accounts collection documents has provider=null. You can modify findOne condition the following way to avoid it:

collection.findOne({'email': fbEmail, 'provider': {$exists: true}}, ...

Note, that this modification will just skip all accounts without provider field set (or with set to null).

Vladimir
  • 9,913
  • 4
  • 26
  • 37
  • sorry if im not fully understand your note, i still need to perform the first `collection.update` when facebook id in the provider is `null`, but if i skip all accounts without `provider` field set then Im not sure how to perform the above action again. – nihulus Jan 23 '13 at 18:21
  • I think i solved it =) just place the create new email outside the `findOne` and use `{$exists: true}` as you said, thank you! – nihulus Jan 24 '13 at 08:12
  • My intention with the note was to warn you about the case when no provider is given. Anyway I'm happy you've solved the problem. You're welcome! – Vladimir Jan 24 '13 at 09:12
  • actually there is still a problem, i still need to create new data if the email doesnt exist, looking at the mongodb query doc... – nihulus Jan 24 '13 at 09:25
  • In this case I'm usually fetching account (`findOne`) (if it exists), then processing or creating retrieved object and finally saving (`save(usert=true)`) it. – Vladimir Jan 24 '13 at 09:33
  • yes i have the workflow in the head, just the queries are confusing me – nihulus Jan 24 '13 at 09:43