mongo native not creating data

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);
                }                
            });             
        }
    });
});

Answers


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).


Need Your Help

Drupal 7 image field size and crop on upload

drupal image-processing drupal-7

I created a module that pulls images into a slider using the nivo jquery plugin, but when I upload the image, and select the image style that formats the size as I want, it doesn't actually change ...

Issue with the Python Eve TokenAuth Feature

python mongodb flask eve

I am currently testing the python-eve library since a few days to create a restful API but I'm experiencing some issues when I follow this tutorial to implement a "Token Authentication".

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.