1

A user has the ability to add languages. languages are stored as an array of type teach or learn in the userschema. I can simply add a language to the languages.teach[] by using push, but how do i remove one?

language object example

let language = {
    "code": FR
    "level": 1
};

UserScema.js

var UserSchema = new Schema({

        email: {
            value: {
                type: String,
                lowercase: true,
                //unique: true,

            },
            token: String,
            verified: Boolean,
        },
        password: {
            type: String,
        },

        phone: {
            countryCode: {
                type: String,
                //required:true,
                unique: true,
            },
            number: {
                type: String,
                required: true
            },
            code: String,
            verified: {
                type: Boolean,
                default: false
            },
        },

        jwt: String,

        profile: {
            username: String,
            firstname: String,
            lastname: String,
            dob: String,
            level: Number,
            location: String,
            image: String,
            introduction: String,
        },
        languages: {
            teach: [],
            learn: [],
        }
    },
    {
        timestamps: {createdAt: 'created_at', updatedAt: 'updated_at'}
    });

LanguagesController.js

destroy(req, res) {

    let id       = req.params.id;
    let language = {
        "code": req.body.code,
        "level": req.body.level
    };
    let type     = req.params.type;

    User.findOne({'_id': id}, function (err, user) {

        if (err) {
            return res.status(404).json({

                success: true,
                status: 404,
                data: err,
                message: "User does not exist",

            });
        }

        if (type === "teach") {

            for (let i = 0; i < user.languages.teach.length; i++)
                if (user.languages.teach[i].code === language.code) {
                    user.languages.teach[i].remove();
                    break;
                }

        }
        if (type === "learn") {
            //user.languages.learn.push(language);
        }

        console.log(user);

        user.save((err, user) => {

            return res.status(200).json({

                success: true,
                status: 201,
                data: user,
                message: "Successfully Deleted Language",

            });
        });

    });

}

I tried to use .remove but im getting a user.languages.teach[i].remove is not a function.

3
  • try user.languages.teach = user.languages.teach.filter( o => o.code !== language.code) instead of your for loop inside if (type === "teach") Commented Sep 17, 2017 at 9:55
  • what does that do, im using type because i want to change the language array depending on user input, use the same code if thy want to manipulate type == teach or type ==learn.. Commented Sep 17, 2017 at 10:02
  • sorry if i was not clear, but i am not saying to remove your if condition, i was saying to remove your for loop inside the if (type === "teach") with my above given snippet. Commented Sep 17, 2017 at 10:04

2 Answers 2

2

Yes .remove is nothing on the array. You can use .filter to remove unwanted array elements.

It would be used like:

if (type === "teach") {
    user.languages.teach = user.languages.teach.filter( o => o.code !== language.code)
}

This will remove all the element from the array with the condition given above.

Read more about Array.prototype.filter.

Sign up to request clarification or add additional context in comments.

1 Comment

Just wondering now if subdocuments would be a better approach?
1

Maybe something like...

...
if (type === "teach") {
  var index = user.lanauges.teach.findIndex(function(item){
     return item.code === languge.code;
  });

  user.languages.teach.splice(index, 1);

  ....

1 Comment

Hi, ty this worked and was easy to understand. but i went with Raghavgarg approach as it was much simpler.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.