After I manually change a field name, the console tells me that the associated value is undefined

After I manually changed a field name in mongoDb, my crud operations no longer work. I have read that in this case the old and new field name in mongoose. Schema must be specified, and have done so, but the database still does not take the values. Do I also have to specify something in my routes in node?

That is the route in node:

 router.post("/", upload.single("img") ,verifyTokenAndAuthorization, async (req,res)=>{
    
    try{ 
        const result = await cloudinary.uploader.upload(req.file.path, {
            upload_preset: "Mern_redux-practice",
            resource_type: "auto",
        }) //cloudinary gets data, so the data must be here
        const newDayLinks = new Daylinks({
            cloudinary_id: result.public_id, 
            title: req.body.title,
            content:req.body.content,
            img: result.secure_url,
            ident: req.body.ident, //this is the new fieldname and it is undefined
        })
        const savedDayLinks = await newDayLinks.save();
        res.status(200).json(savedDayLinks);
    } catch(error){ 
        res.status(403)
        console.log(ident, title, content,img);
        throw new Error("Action failed"); //this error is thrown
    }
});

That is the current mongoose Schema:

const DaylinksSchema = new mongoose.Schema({
    cloudinary_id: {type:String, required:true},
    img:{type:String, required:true},
    title:{type:String, required:true},
    content:{type:String, required:true},
    id:{type:String}, //that was the fieldname before
    ident:{type:String, required:true}, // that is the new fieldname I manually changed in the collection
}, 
    {timestamps:true},
);

module.exports = mongoose.model("Daylinks", DaylinksSchema);

Hi @Roman_Rostock

Could you elaborate on this issue a bit further?

  • What do you mean by “the database does not take the values”? Do you mean you instruct mongoose to save the document but the updated document is not saved?
  • What exactly did you change manually in the database, and how?
  • We’re not really experts in Cloudinary, but we may be able to help with mongoose. Can you post a code example that you expect to work but didn’t, what is your input, and what is your expected output?

Would be great if you can also post your MongoDB version and mongoose version as well.

Best regards
Kevin

1 Like

Hello kevinadi, I have got an answer on stack overflow. I must drop the collection and create it new. I have not done it yet, but how I learned in m001basic, I will drop the collection in the ui and create a new post, so the collection should be there again. I found out that I have an dublicate key error. When I post an entry the first one is uploaded, but when I want to post a second one, it throws me that error.

in mongodb, you do not need to drop any collection just because you have changed a field in your schema. that is how documents databases work; you don’t need a concrete shape to save data.

on the other hand, you need a schema just because the application you use may use an ORM or DAO library to map the data to objects. yet this does not means you need to remove/recreate/migrate all previous data when you have a design change.

your old data still pretty much usable, it is just that your app does not know how to handle the change. for example, you can add a new field “version” to your new document schema to differentiate old and new, and use 2 schema. or you can add both fields in one schema, check for existence of old field but write new data with new field. and if you are the sole user of the data, you can invoke an update to all existing documents from" “id” to “ident”. make note that none of these include a data removal, because mongodb gives all these kinds of freedom.

2 Likes