User Preferences Ticket - unable to solve this challenge

I am not able to solve the “User Preferences” assignment since a couple of days, can someone tell me whats wrong with my code?
Here it is:

static async updatePreferences(email, preferences) {
try {
  /**
  Ticket: User Preferences

  Update the "preferences" field in the corresponding user's document to
  reflect the new information in preferences.
  */

  preferences = preferences || {}

  // TODO Ticket: User Preferences
  // Use the data in "preferences" to update the user's preferences.
  const updateResponse = await users.updateOne(
    { user_id: email },
    { 
      $set:  { 
        preferences: {
            favorite_cast: 'Tom Hanks',
            preferred_language: 'English'
          }, 
      }
    }
  )

  if (updateResponse.matchedCount === 0) {
    return { error: "No user found with that email" }
  }
  return updateResponse
} catch (e) {
  console.error(
    `An error occurred while updating this user's preferences, ${e}`,
  )
  return { error: e }
}

}

Why do you use hard coded values for preferences when you are given the wanted preferences as a parameter?

As suggested by @steevej-1495 you don’t have to hard code preferences. You have to pass preference to preference key. Also if you check users schema in compass the field should be email instead of user_id

Thanks for your reply. I again tried different options, as follows, but none of them pass!

Code 1:

// TODO Ticket: User Preferences

  // Use the data in "preferences" to update the user's preferences.

  const updateResponse = await users.updateOne(

    { email: "testUser.email" },

    { 

      $set:  { preferences: "newPreferences" }

    }

  )

Code 2:

// TODO Ticket: User Preferences

  // Use the data in "preferences" to update the user's preferences.

  const updateResponse = await users.updateOne(

    { email: email },

    { 

      $set:  { preferences: "newPreferences" }

    }

  )

Code 3:

// TODO Ticket: User Preferences

  // Use the data in "preferences" to update the user's preferences.

  const updateResponse = await users.updateOne(

    { email: "foobaz@bar.com" },

    { 

      $set:  { preferences: "newPreferences" }

    }

  )

P.S: I cannot see the schema in my compass as suggested by you. Cannot figure this out!

Thanks for your reply but I still haven’t solved this. But I do appreciate your help.

It should be like this

  const updateResponse = await users.updateOne(

    { email: email },

    { 

      $set:  { preferences: preferences }

    }

  )

You again hard coded some of the values. For hint you can read the comments of the code. Just have to pass the function parameters here.

Hope you have given correct connection string as mentioned in the beginning of the session.

2 Likes

Thanks a ton. It finally passed! Really appreciate your help.

2 Likes