Sorry, but mongoose isn't something you can call

Encountering the error: TypeError: friendData.insert is not a function. Need assistance desperately. I've tried modifying the methods, which resulted in successful drops. Feeling helpless and clueless on how to proceed further with this issue.


const FriendSchema = new Schema({
    user: {type: Schema.Types.ObjectId, ref: 'User'},
    email: {type: String, unique: true, required: true},
    friends: {type: Array},
    subscribe: {type: Array},
    requestFrineds: {type: Array},
})

module.exports = model('Friends', FriendSchema);


class friendService {
    async addFriend(email, sender) {
        const friendData = await FriendModel.findOne({email: email})
        const senderData = await FriendModel.findOne({email: sender})
        console.log(friendData, senderData)
        await friendData.insert({requestFriends: sender});
        await senderData.insert({subscribe: email});
        friendData.save();
        senderData.save();
        // return friends;
    }

Answer №1

If you are looking to add elements to an array in JavaScript, the push() method is what you need instead of an insert method.

class friendService {
    async addFriend(email, sender) {
        const friendData = await FriendModel.findOne({email: email})
        const senderData = await FriendModel.findOne({email: sender})
        console.log(friendData, senderData)
        friendData.requestFriends.push(sender);
        senderData.subscribe.push(email);
        await friendData.save(); //< await the save()
        await senderData.save(); //< await the save()
        // return friends;
    }

Note 1: Remember to use the await keyword with the save() method.

Optimizing your code by using findOneAndUpdate() can help reduce the number of database requests in this scenario.

Note 2: Check for typos like the misspelling of requestFriends in your schema definition.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Visuals failing to display following Angular project compilation

After completing the coding for my app, I attempted to put it into production mode and after testing, I noticed that the images were not displaying as the logo in the app. However, in development mode, everything seems to be working fine. This is the struc ...

Guide to selecting a specific year on a calendar using Selenium with JavaScript

While attempting to create a Selenium test using JavaScript, I encountered an issue with filling in calendar data through a dropdown menu: const {Builder, By, Key} = require('selenium-webdriver') const test2 = async () => { let driver = awa ...

Is it possible to incorporate an HTML5 YouTube video without using an iframe tag

Can an HTML5 version of a YouTube video be embedded without the need for an iframe? ...

Having trouble getting past the initial iteration in Angular's modal system?

Having trouble generating a modal for every iteration in ngFor. The data passed to the modal seems to be stuck in the first iteration and doesn't change with each iteration. Any ideas on how to solve this? <div class="container"> & ...

Guide on generating a dynamic table by looping through an array and placing values inside <tr> tags, then saving it in a variable

I am working with an array object and need to dynamically create a table where each tablerow (tr) pulls values from the array object. After creating the table, I want to store it in a variable so that I can pass it to a rest call, triggering an email with ...

The error message states: `discord.js TypeError: Unable to access the property 'resolve' as it is undefined`

Encountering an issue with the following code snippet const Discord = require('discord.js'); module.exports = { name: 'info', description: "Shows BOT's Informations", execute(message, client, args) { c ...

jQuery cannot access a hidden CSS property

I'm working on a form for my new project and have assigned the same class to all input fields. In my CSS file, I've set the text to be capitalized and it displays correctly. However, when I use jQuery's .val() to get the value of the field, ...

Maximum number of days that can be selected with Bootstrap Datepicker

I currently have a datepicker set with the multidate option and I am looking to specify a maximum number of days that users can select, say 5 days. Once a user has selected 5 days, any additional days should become disabled dynamically. How can this be a ...

What is the best way to test an AngularJS directive and monitor function calls with a spy?

After running the code below, an error is thrown mentioning that element.popover is not being invoked. I can't seem to identify what the problem is. Thank you in advance for any assistance provided. directive: angular.module('directives', ...

How can I integrate vue-cli output into a PHP project?

One benefit of using vue-cli is that it automatically sets up a local server for you. I'm curious, can I utilize the output from vue-cli in a PHP project (such as app.js )? Another question I have is related to the local network - How does it suppo ...

The surprising behavior of Rails rendering partials even when they are commented out has

I'm intrigued by how Rails 5 handles partials and if there might be a hidden problem I haven't encountered yet. On my current page, I have two partials - one that is included in the HTML itself, and another that is supposed to render inside an aj ...

Using a node module for Three.js path manipulation

Currently, I am in the process of learning Three.js and have set up a basic project that runs on a node.js server while importing Three.js as a node module. Although my setup is working fine, I find myself a little bit confused about whether this is consi ...

Retrieve the desired element from an array when a button is clicked

When I click on the button, I need to update an object in an array. However, I am facing difficulties selecting the object that was clicked. For better readability, here is the link to my GitHub repository: https://github.com/Azciop/BernamontSteven_P7_V2 ...

How can I prevent users from inputting negative numbers in a cellEditable material-table?

In my project, I am utilizing a material table with editable cells. I need to implement a restriction that prevents users from entering negative numbers in the field and displays an error validation message. How can I achieve this functionality? Check out ...

Having difficulties with JavaScript's if statements

I'm facing an issue with my JavaScript code that is meant to modify the value of a price variable and then display the updated price. The problem arises when I have multiple select options, as the price only reflects the ID of the last statement. Here ...

Complicated scenario involving distinct identifiers and dynamically generated items

I am facing an issue with a button that, when clicked, adds a new media item to a list. The problem is that it uses unique IDs that end up getting duplicated. I am looking for a solution to add some kind of anonymous number to the ID to prevent duplication ...

Highlighting the active menu item in Angular with Bootstrap

On the main page, I have the following code snippet for navigation: <li ng-class="{ active: hello.isActive('/page1')}"><a ui-sref="root.content({store:hello.store_selected,product:hello.product_selected})" >page1</a></li> ...

Using AngularJS to dynamically bind HTML content based on a variable’s value

I am trying to dynamically display an image of a man or a woman on a scene, depending on the gender of the person logged into my website. The ng-bind-html directive is working fine when I define "imageToLoad" statically. However, it fails when I try to gen ...

Utilizing Material CSS to display tooltips upon hovering over the floating-action-button

Context: My task involves revamping an outdated system, with the main focus being on enhancing user experience. A key element of this initiative is the introduction of floating action buttons (FABs) to streamline navigation and interaction. However, given ...

Steps for inserting a JSON Array into a database

I have a dropdown menu that displays different options based on the selection from another dropdown. The data for each dropdown is fetched from the database and I need to insert the selected values into a new table in the database. All the necessary code ...