Access user.services field

Hello Guys.

I am trying to access the “services” field of Meteor users collection through nodejs like this

const user = await User.findOne({ "emails.0.address": req.body.email });

I can see the “services” field when when I console log “user”

{ _id: 'zn5G3T9pBeySXdv9h',
  createdAt: 2019-10-11T03:56:21.554Z,
  services:
   { password:
      { bcrypt: '$2a$10$OjJZwMKoTAWPBJ.sVAhlauq34DeOMB8DHCg3sWe3/.ghnheoV9vnqFTi' },
     email: { verificationTokens: [] },
     resume: { loginTokens: [Array] } },
  roles: { Active: [ 'userRole' ] },
  profile:
   { fullName: 'John Doe',
     title: 'Manager',
     type: 'userr',
     talentPool: [] },
  emails: [ { address: 'user@company.com', verified: true } ] }

But when I try to access “user.services” I get undefined. I developing a application outside of MeteorJS and it uses the data from the users collection to authenticate users and the application is being developed using ReactJS and nodejs. Any suggestions on how can I do this ? Thanks in advance.

maybe remove the await keyword will work.

I don’t think await has something to do with it.

have you tried? Because you don’t have to use await with findOne function.

tried now … same result

Can you try with the fields option?

const user = await User.findOne({ 'emails.0.address': req.body.email }, { fields: { services: 1 } });

From what I understand, you’re not running this with meteor, but just nodejs with the promise based mongodb client I would guess, if that’s the case, then await should be fine… but if User is a Meteor collection, then you do not need to await for the result.

Thanks @pcastrom … I am using mongoose with Nodejs for MongoDB operations. I have tried your suggestion and I am getting this error

UnhandledPromiseRejectionWarning: MongoError: Can't canonicalize query: BadValue Unsupported projection option: fields: { services: 1 }

Tried this way

const user = await User.findOne({
    "emails.0.address": req.body.email,
  }).select({ services: 1 });

getting this

{
    "_id": "zn5G3T9pBeySXdv9h",
    "services": {
        "password": {
            "bcrypt": "$2a$10$OjJZwMKoTAWPBJ.sVAhlauq34DeOMB8DCg3sWe3/.ghnhV9vnqFTi"
        },
        "email": {
            "verificationTokens": []
        },
        "resume": {
            "loginTokens": [
                {
                    "hashedToken": "IagmDjRHiAAVmQ9HN8xhfDf8LyFlx475o+mXcoIg6So=",
                    "when": "2020-02-05T18:15:39.149Z"
                },
                {
                    "hashedToken": "yv5aq7maa3u8+aeArptYGIbr4gOJ3+QGVgzef8NXmsM=",
                    "when": "2020-02-07T11:28:55.110Z"
                },
                {
                    "hashedToken": "MbGwZBzDqdhCHdCmNXYRAi8C5asULE/z7aONz0yDTH0=",
                    "when": "2020-02-07T11:30:39.597Z"
                },
                {
                    "hashedToken": "TxTipJusCDk6AJG4YwagWP0IWqlmNpdYJckx3rf78Ns=",
                    "when": "2020-03-12T10:11:40.288Z"
                },
                {
                    "hashedToken": "Ijq6EImxcKgOT+5lg7AerOj7rNsM4/Tk+saRlhTfNgo=",
                    "when": "2020-03-12T20:16:35.839Z"
                },
                {
                    "hashedToken": "c7VmJxt3AwvEjoY2SYxo98PvX5PQUzmBlj0h6oPUF3U=",
                    "when": "2020-03-16T17:40:52.502Z"
                },
                {
                    "hashedToken": "vCDdM/ATrF4pibNM4C/INZvUxYm33pLvBLCaZ4NNvf4=",
                    "when": "2020-03-19T18:36:46.061Z"
                },
                {
                    "hashedToken": "uY3hMGHSAxREoJLQhqJIwHDiktw8D1h8H3bkyFYZfNk=",
                    "when": "2020-03-30T12:19:41.955Z"
                },
                {
                    "hashedToken": "ZTfnUrmyFY7JgDJAT4XA7xlcllfP22mbDlfICdy6yI4=",
                    "when": "2020-04-06T06:53:35.557Z"
                }
            ]
        }
    }
}

but again when I console log “user.services” I get “undefined”

If you’re querying a mongodb collection using mongoose, then what does Meteor has to do with this?

Please read my question. I am querying mongo using mongoose I am getting this object in return like I should

{ _id: 'zn5G3T9pBeySXdv9h',
  createdAt: 2019-10-11T03:56:21.554Z,
  services:
   { password:
      { bcrypt: '$2a$10$OjJZwMKoTAWPBJ.sVAhlauq34DeOMB8DHCg3sWe3/.ghnheoV9vnqFTi' },
     email: { verificationTokens: [] },
     resume: { loginTokens: [Array] } },
  roles: { Active: [ 'userRole' ] },
  profile:
   { fullName: 'John Doe',
     title: 'Manager',
     type: 'userr',
     talentPool: [] },
  emails: [ { address: 'user@company.com', verified: true } ] }

But when I try to extract data from the “services” field in that object like this “user.services” I get “undefined” in return. But you can see that the “services” field is there and it contains other objects. What might be causing this.

My question was to find out why I am getting undefined. Is “services” is some kind of secure key I might be wrong but I am trying to find out.

I read your question, but again you’re not using Meteor users or collection. If I understood correctly, you’re question is pertaining the usage of Mongoose ODM in a NodeJS environment outside of Meteor is that correct?

Yes that is correct. I am trying to find out if this is linked to any encryption that accounts-password might have added. to be honest I really doubt that.

Yeah I don’t think there is anything special about that field, it’s just an array.

You might want to check the mongoose schema perhaps? here is an example:

// Define userSchema
const userSchema = new Schema(
  {
    _id: { index: true, type: String, unique: true, required: false },
    services: {
      password: {
        bcrypt: { type: String }
      }
    },
    createdAt: { type: String, unique: false, required: false },
    username: { type: String, unique: false, required: false },
    password: { type: String, unique: false, required: false }
  },
  { strict: true }
);

Also I’m not really sure why you need the service field it is only used within Meteor accounts system (that’s why it’s not defined in the schema above).

Edit: sorry I mean the resume array, the service has the password, you can use the schema I’ve provided.

Yeah there is nothing special about the field and I think you might be right about the schema.
need the password. developing another application that will use our existing users which are registered through a MeteorJS application

Yes I get it, the schema above works, you need to add it to mongoose.

I will try and get back to you.

Thanks @alawi your suggestion worked. Thanks

1 Like