Trigger Function //TypeError: Cannot access member 'id' of undefined

I’m trying to create a file when a User will the first time be authenticated. I use the down below function. Somehow I am getting the following error: “TypeError: Cannot access member ‘id’ of undefined”. I don’t know why I can’t access the user.id.

    exports = function(authEvent){
          const mongodb = context.services.get("mongodb-atlas");
          const users = mongodb.db('data').collection('User');
          const { user } = authEvent
          const newObjcId = new BSON.ObjectId()
          
          const newUser = {
            _id: newObjcId,
            realm_id: user.id
          };

      
      return users.updateOne({_id: newObjcId}, newUser, {upsert: true});
    };

Here’s a snippet of an auth trigger I have that successfully uses user.id – maybe copy that syntax?

exports = function({user}) {
  const db = context.services.get("mongodb-atlas").db("RChat");
  const userCollection = db.collection("User");
  
  const partition = `user=${user.id}`;

This was for email/password authentication when a new user registers.

What authentication and event types is your trigger running for?

1 Like

Hey, it’s an apple and google authentication event when a user authenticate the first time. It still says “TypeError: Cannot access member ‘id’ of undefined”