The function .save is not valid and the function .map is unavailable when attempting to update a document

Here is the code I am working with: When I attempt to run it in Postman, I receive the following error message:

"message": "account.save is not a function"

const account = await Account.find({ "buildings.gateways.devices.verificationCode": code })
    var accountId = account ? account.map(item => item._id) : null

const buildings = _.flatMap(account, a => a.buildings)
const gateways = _.flatMap(buildings, b => b.gateways);
const devices = _.flatMap(gateways, g => g.devices);

// finding deviceId to insert for user from that account
const device = _.filter(devices, d => d.verificationCode === code);

device.patientFirstName = req.body.firstName;
device.patientLastName = req.body.lastName;
account.save();

When I attempt to switch to using findOne(), a new issue arises:

"account.map is not a function"

I always get values from account, so why can't I map them? It's puzzling to me.

Your assistance would be greatly appreciated. Thank you

Answer №1

.find() will give you an array where you can use Array.map(), but you must remember to save each document individually using .save():

const accounts = await Account.find({ "buildings.gateways.devices.verificationCode": code })
var accountId = account ? account.map(item => item._id) : null

for(let account of accounts){
    await account.save();
}

.findOne() (awaited) gives you a single document, so you cannot directly use .map():

const account = await Account.findOne({ "buildings.gateways.devices.verificationCode": code })
var accountId = account ? account._id : null

account.save();

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

"Searching for a specific document in a MongoDB nested multi-dimensional array using elemMatch

These are the contents of my files: //file 1 { info : [ { id : 100, field : { a : 1, b : 2 } }, { id : 200, fi ...

Determine if a Vimeo video successfully loads within an iframe using jQuery

I'm currently working on a form where users input a Vimeo video ID and the page displays a preview of the video in an iframe. How can I verify if the video exists? This is the JavaScript code used to load the iframe: $(document).on('change ...

Seeking the Bluebird promise method "any" within a sequence

One of my requirements involves running Promise.any in series. Since Promise.any is not available (correct me if I'm wrong), I created a function to execute promises sequentially using Pomise.mapSeries and resolve at the first satisfied test. The pr ...

Encountering a jQuery error in IE8: "Null is either null or not an object"

Whenever I include jquery with the following code: <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> it functions properly on IE9, firefox, chrome, and safari but encounters issues ...

What is the best way to create a drop-down menu that exports data directly to an Excel spreadsheet?

I have been struggling with a seemingly simple issue - I can't seem to get my drop-down box to display the chosen option. The code I'm using is quite similar to this generic one, but for some reason, it's not reporting the selected option to ...

Is there a way to execute a JavaScript function on a webpage using Selenium automation?

Here's an element on a website: <span class="log-out-ico" ng-click="logout()"> Instead of clicking it, I want to run the "logout()" script from selenium. Is that possible? If so, how can I do it? This is what I attempted: I ...

Creating a Gulpfile.js that efficiently manages a multitude of files spread across various folders

For my ongoing WordPress projects, I am simultaneously working on themes and plugins. In my theme folder, I have a gulpfile.js that compiles .scss files into .css. I am considering creating a central "master" gulpfile in the root folder to compile .scss fi ...

Searching for the parent field using a Basic Query in Spring MongoDB @DBRef

Is there a way to query mongodb in order to search for all publications where the author's first name is "Vinod"? Below is my Publication Class @Document(collection = "publications") public class Publication { @Id private String id; pr ...

What is the reason that selection disappears when right-clicking on highlighted text to bring up the context menu?

Currently, I am utilizing Material UI's context menu implementation in my project. On Chrome browser, everything is functioning flawlessly, as depicted below. Chrome: https://i.stack.imgur.com/i7eA1.gif However, I have encountered a peculiar issue ...

Struggling with passing a function along with parameters to a component in React has been a challenge for me

Currently utilizing React in conjunction with NextJS My goal is to send a function, along with its parameters, to my 'Alerts' component so that it can wait for user input before executing the function. For instance, prior to clearing a list, I ...

Struggling to implement React.RefObject in TypeScript

I am currently in the process of converting this class module to Typescript, but I am having trouble defining the object reference correctly. To view the example, click here. My current definition for the reference holder is as follows: private wrapperRe ...

Find the percentage of wins in MongoDB by grouping and counting

Among the data I possess {"id" :1,"win":100}, {"id" :1,"win":150}, {"id" :1,"win":10}, {"id" :1,"win":0}, {"id" :2,"win":100}, {"id" :2,"win":0}, {"id" :3,"win":100}, {"id" :3,"win":200}, I am tasked with determining the winning percentage for each id; ...

Securing user credentials with encryption in a linux shell script

Is there a way to securely store user and password credentials for an automated shell script that interacts with MongoDB? Currently, the username and password are stored in plaintext within the script. I'm exploring options to encrypt the password eit ...

When the boolean in RxJS skipWhile remains true

My current scenario involves a specific use-case: There is a service that queues calculation tasks assigned with a certain ID. I have set up an observable to monitor the status of the service. When the ID is still in the queue, the query result is true (i ...

Guide to effectively retrieving data from S3 and displaying a view at regular intervals of 4 seconds

In my current project, I am working on fetching a file from S3 and utilizing the data within that file to generate a map on a webpage. To achieve this task, I have set up an Express server along with the EJS templating engine for serving and rendering the ...

JS use regex to set input field value

Is there a way to automatically format the input value in an HTML time picker field using regex and the replace function? For example, when a user enters numbers, the input value should be formatted accordingly based on a specific pattern. I attempted to ...

Rendering components before ComponentDidMount runs and Axios handles state updates

When I try to pass the gifs state to my GifList component in the render method, I encounter an issue. It seems that when trying to access the array within the component through props, it is returning as undefined. After investigating further, I noticed t ...

The loading of the module from was hindered due to an invalid MIME type restriction

Exploring a three.js example and encountering an issue when utilizing import within a JavaScript module. The error message displayed is: Loading module from “http://localhost:8000/build/three.module.js” was blocked because of a disallowed MIME type ( ...

Is there a way to insert a row into a datatable without needing to perform an Ajax reload or using the

When using row.add(…) on a datatable, I encounter an issue where it refreshes via an ajax call when draw() is activated. This leads to the new row not being visible because the data is reloaded from the database. The UX flow behind this scenario is as f ...

Error message: "Attempting to update /mytable using the `UPDATE` SQL command on React App localhost:MYPORT with react-postgres and PostgreSQL / node.js backend is not allowed."

Following the tutorial located at https://github.com/nsebhastian/react-node-postgres, I have successfully implemented the full guide, as evidenced by the results documented in TypeError: Cannot read property 'rows' of undefined. The functionalit ...