skip to Main Content

So I have a schema right now for a users profile and I want to update the media related to that profile and store it mongodb the way we identify a user is through their username as they are unique on the site I tried the push method but it doesn’t seem to work, the profile is found correctly just trying to update the existing field object we don’t want to override what is stored at that object either just add to what is already there.

here is the schema:

const mongoose = require("mongoose");


const medallionProfileSchema = new mongoose.Schema({
  firstName: {
    type: String,
    required: true,
    unique: false,
  },
  middleName: {
    type: String,
    required: false,
    unique: false,
  },
  lastName: {
    type: String,
    required: true,
    unique: false,
  },
  title: {
    type: String,
    required: false,
    unique: false,
  },
  relationship: {
    type: String,
    required: false,
    unique: false,
  },
  media: {
    title: {
      type: String,
      required: false,
    },
    description: {
      type: String,
      required: false,
    },
    mediaType: {
      type: String,
      required: false,
    },
    mediaLink: {
      type: String,
      required: false,
    },
  },
});

const profileSchema = new mongoose.Schema({
  username: {
    type: String,
    required: true,
    unique: true,
  },
  email: {
    type: String,
    required: true,
    unique: true,
  },
  dateCreated: {
    type: Date,
    required: false,
    unique: false,
  },
  bio: String,
  // Add other profile-related fields as needed
  medallionProfile: medallionProfileSchema, // Embed MedallionProfile schema


});


const Profile = mongoose.model("Profile", profileSchema);

module.exports = Profile;

here is the function to carry out the update the req.body params are present and correct:

const uploadProfileMedia = async (req, res) => {
  try {
    // Extract data from the request body
    const { username, title, description, mediaType, mediaLink } = req.body;

    // Find the profile document associated with the provided username
    const profile = await Profile.findOne({ username });
    console.log("profile in uploading media ", profile);

    // If the profile doesn't exist, return an error
    if (!profile) {
      return res.status(404).json({ error: "Profile not found" });
    }

    // Add the new media to the profile
    profile.medallionProfile.media.push({
      title,
      description,
      mediaType,
      mediaLink,
    });

    // Save the updated profile document
    await profile.save();

    // Respond with a success message
    res.status(200).json({ message: "Media uploaded successfully" });
  } catch (error) {
    // Handle errors
    console.error("Error uploading profile media:", error);
    res.status(500).json({ error: "Internal server error" });
  }
};

2

Answers


  1. You will have to define embed schema in an Array type as you are trying to push.

    medallionProfile: [medallionProfileSchema]
    
    Login or Signup to reply.
  2. In your medallionProfileSchema you have defined the media property as an object here:

    const medallionProfileSchema = new mongoose.Schema({
      //...
      media: {
        title: {
          type: String,
          required: false,
        },
        description: {
          type: String,
          required: false,
        },
        mediaType: {
          type: String,
          required: false,
        },
        mediaLink: {
          type: String,
          required: false,
        },
      },
      //...
    });
    

    But you are trying to use the array push() method here:

    profile.medallionProfile.media.push({
          title,
          description,
          mediaType,
          mediaLink,
        });
    

    Option 1:

    If your intention was to have media as an array of media objects you will need to update your medallionProfileSchema with array syntax [] like so:

    const medallionProfileSchema = new mongoose.Schema({
      //...
      media: [{
        title: {
          type: String,
          required: false,
        },
        description: {
          type: String,
          required: false,
        },
        mediaType: {
          type: String,
          required: false,
        },
        mediaLink: {
          type: String,
          required: false,
        },
      }],
      //...
    });
    

    Option 2:

    If your intention was to have media as a simple object then you cannot use push(). You can just assign new values to the key like so:

    profile.medallionProfile.media = {title, description, mediaType, mediaLink};
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search