1

I have the following schema:

var userSchema = new Schema({

        userID: Number,
        userName: String,
        userEmail: String,
        teams:Array,
        socialMedias:
        {
        fbUID: String,
        googleUID: String,
        twitter: String }


});

First, How can I add an empty array? Is it right the way I am doing in the following?

teams:{},

Second, I am trying to do a query using Mongoose in my Node.js but I am getting an error in the dot ('.'):

This is my document I am saving:

 var user = new users({
        userID: id, //give the id of the next user in Dbase
        userName: userName, 
        userEmail: 'userEmail',
        teams:{},
        socialMedias: [{socialMediaType: socialMediaID}]
     });

where userName, socialMediaType and socialMediaID are parameters of a function.

So, after I add this doc, I am trying to do the following query:

function searchUser(socialMediaID, socialMediaType){
    var user

     users.findOne({socialMedias.socialMediaType: socialMediaID}, function(err, userFound){

        if(err) return handleError(err);
       user = userFound; 
     });

     //what does  MongoDb return if it does not find the document?

     return user;
}

but I am getting an error in this :

socialMedias.socialMediaType

So, how can I do this query?

I tried to find in Mongoose Documentation but I did not find.

Thank you for your understanding.

Andressa Pinheiro
  • 1,517
  • 2
  • 18
  • 28
  • To be valid JavaScript at least, you'll have to quote the key since it isn't an identifier -- `{"socialMedias.socialMediaType": socialMediaID }`. – Jonathan Lonowski Jul 14 '14 at 19:56
  • But I cannot quote it because this key is received by the function. So it can be 3 possible name. – Andressa Pinheiro Jul 14 '14 at 20:03
  • 1
    For that, object literals/initializers don't evaluate keys as variables. The name of the identifier itself becomes the key. [You'll have to build the object with bracket notation.](http://stackoverflow.com/questions/2462800/how-to-create-a-dynamic-key-to-be-added-to-a-javascript-object-variable) – Jonathan Lonowski Jul 14 '14 at 20:05
  • ok, I will try to do this. Just dont know how, since I dont know which word I am receiving. – Andressa Pinheiro Jul 14 '14 at 20:07
  • @JonathanLonowski , Do I have to quote it with double quotation marks or just one (')? I am asking because in MongoDB Documentation is just one like 'fsfe' and in your comment it's two like "srrr". – Andressa Pinheiro Jul 16 '14 at 13:36
  • 1
    In JavaScript, [you can use either.](http://stackoverflow.com/questions/3149192/difference-between-single-quotes-and-double-quotes-in-javascript) The only difference is that the quotation used for the string needs to be escaped when included as a character -- [`'Joe\'s'` vs `"Joe's"`](http://stackoverflow.com/questions/242813/when-to-use-double-or-single-quotes-in-javascript). Otherwise, they're identical. – Jonathan Lonowski Jul 16 '14 at 15:52

1 Answers1

1

There's a number of issues here that you are likely running into.

First, teams is an array property, but you're assigning an object to it. You need to do something like this:

var user = new users({
    userID: id, //give the id of the next user in Dbase
    userName: userName, 
    userEmail: 'userEmail',
    teams:[],
    socialMedias: [{socialMediaType: socialMediaID}]
});

Second, if socialMediaType is passed in as a function param, you can't use it like you're doing. You need to do something like this:

var socialMedias = {};
socialMedias[socialMediaType] = socialMediaID;
var user = new users({
    userID: id, //give the id of the next user in Dbase
    userName: userName, 
    userEmail: 'userEmail',
    teams:[],
    socialMedias: [socialMedias]
});

Third your findOne is not going to work as is. From what I can gather of your intention here, you need something like this:

function searchUser(socialMediaID, socialMediaType){
    var user
    var query = {};
    query["socialMedias."+socialMediaType] = socialMediaID;

    users.findOne(query, function(err, userFound){

        if(err) return handleError(err);
        user = userFound; 
    });

    //what does  MongoDb return if it does not find the document?

    return user;
}

But fourth, even that won't work because you are synchronously returning user from a method that performs and asynchronous operation. There are various ways to solve that, but you can start by reading up about promises, or passing a callback function into searchUser.

user1417684
  • 2,584
  • 1
  • 19
  • 14
  • If you can, could you explain Why 'socialMedias' would not work the way it is? – Andressa Pinheiro Jul 15 '14 at 15:13
  • 1
    When you say users.findOne({socialMedias.socialMediaType: socialMediaID}, ...), the part to the left of the : is invalid as is, it needs to be quoted. However, I don't believe you can do this directly in the anonymous object definition as you had originally. So the temporary object query is used to do that. – user1417684 Jul 15 '14 at 15:23
  • Thank you. Just one more question:What do you mean "reading up about promises, or passing a callback function into searchUser"? – Andressa Pinheiro Jul 15 '14 at 15:38
  • 1
    I mean that those are two ways in which to handle the async operation that you need to accomplish here. For promises, I suggest Q: https://github.com/kriskowal/q. – user1417684 Jul 15 '14 at 15:48
  • Thank you again. I read about Promisses but it is too complicated for this simple thing. Do you think I really need to use Promisse in this function? how about a callback? – Andressa Pinheiro Jul 15 '14 at 20:30
  • 1
    You don't need to, for simple stuff a callback might be fine, but it really depends on your design and preferences. – user1417684 Jul 16 '14 at 16:30
  • Ok. Thank you. Can you help me one more time? I am gonna update my question. I am having some problems with MongoDB. Just this part. – Andressa Pinheiro Jul 16 '14 at 17:49
  • I updated it. If you can help me it would be great. I am taking too long to do it. :/ – Andressa Pinheiro Jul 16 '14 at 17:55
  • You should create a new question for this, as it's sort of veering off the original topic. – user1417684 Jul 16 '14 at 17:56