Tips for ensuring all data is properly set before saving in mongoose

Struggling to update undefined or defined values in Mongoose due to the need to await their assignment. It seems like the code is not saving the data because USER.save() is executed before the values are set. How can I ensure that the data is updated/set before saving it?

const USER = await User.findById(await UserID)

USER.authToken = await authToken,
USER.displayName = await name,
USER.profilePic = await pic
      
await USER.save()

Answer №1

After some troubleshooting, I finally figured it out. Setting the authToken triggered the .save() method, so I ended up adding two more instances of .save() to make everything work smoothly. Now everything is working perfectly and I couldn't be happier with the solution. Apologies for any confusion in my initial question.

USER.authToken = await authToken
await USER.save()
USER.displayName = await name
await USER.save()
USER.profilePic = await pic
await USER.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

Is it possible to transform JSON Array Data from one format to another?

As someone who is new to the software field, I am working with a JSON array of objects: var treeObj = [ { "name": "sriram", "refernce_id": "SAN001", "sponer_id": "SAN000" }, { "name": "neeraja", "r ...

Mongoose Error: The function mongoose.connection is not defined

I'm having trouble connecting to my MongoDB database using mongoose ODM and I keep getting an error message that says: TypeError: mongoose.connection is not a function This is the code snippet for the mongoose function: const mongoose = require(" ...

Exploring the differences between selecting an element by its class and making multiple calls to select elements

I have a question about choosing multiple elements from a page. Is it better to use a selector by class, or make multiple calls using the selector by id? The number of elements I want to select, or unwanted elements that need to be checked by the selector ...

The function google.script.run encounters an error when dealing with file inputs

For the past two years, my Google Apps Script connected to a spreadsheet has been working flawlessly. I created an HTML form to upload CSV and Excel files for processing and data loading into the spreadsheet. However, since March 2020, file uploading has b ...

What steps can I take to troubleshoot the 'Uncaught DOMException: failed to execute add on DOMTokenList' error?

I am looking to create media icons <div class="contact"> <span> <i class="fa fa-phone"></i> </span> <span> <i class="fa fa-facebook"></i> </spa ...

Conditional rendering of a component in ReactJS while maintaining the "empty" space

Is there a way to conditionally render a component without affecting the layout of other components? I'm trying to avoid unwanted shifts caused by this div Do you have any suggestions? Here is a snippet of my code: const [displayFeedbackMessage, s ...

Activate Bootstrap modal using an anchor tag that links to a valid external URL as a fallback option

To ensure accessibility for users who may have javascript disabled, we follow a company standard of developing our web pages accordingly. Our target demographic still includes those familiar with VCRs blinking 12:00. One particular challenge involves a te ...

What is the best way to send data from ajax to a django view?

I have a script for ajax that retrieves the public key and sends other details to the Stripe server for payment. However, I am struggling with sending the dynamic value of the price to item_line. fetch("/config/") .then((result) => { return re ...

Test the file upload functionality of a Node Js application by simulating the process using Chai

My API testing involves receiving a file as input. I have successfully used the attach() function for this purpose. To cover all scenarios, I anticipate using around 20 different input files. Rather than storing these 20 files individually, my idea is to c ...

Exploring Angular 2 Routing across multiple components

I am facing a situation where I have an app component with defined routes and a <router-outlet></router-outlet> set. Additionally, I also have a menu component where I want to set the [routerLink] using the same routes as the app component. How ...

Exploring the intricacies of parsing nested JSON data

Could someone assist me with understanding the following json data? { "Message":"The request is invalid.", "ModelState":{ "model.ConfirmPassword":["The password and confirmation password do not match.","The password and confirmation passwo ...

The XML data format used to make a query on the OCS service in Nextcloud

Looking for a way to obtain a direct download link to a Netcloud file. Instructions: To get a direct link: POST /ocs/v2.php/apps/dav/api/v1/direct Include the fileId in the body (ex: fileId=42). I am trying to make a POST request using Nodes.js. What s ...

Attempting to transmit a ng-repeat object to a personalized filter function

Objective: The goal is to enable a user to input the name of a course into the search field and view a list of students who are enrolled in that course. Data Models: course (contains course name and code) student (holds a list of courses they are regist ...

Troubleshooting Uploadify Issues

UPDATE: I discovered that the problem was related to Uploadify not having a session, which prevented it from accessing the designated page. To avoid this issue, simply direct it to a page without any admin login security ;) The issue stemmed from Upload ...

Generate a byte array in JavaScript

How can a byte array be created from a file in JavaScript (or Typescript) to send to a C# WebApi service and be consumable by the C# byte array method parameter? Could you provide an example of the JavaScript code? The context here is an Angular 4+ applic ...

Using JavaScript, create a regular expression with variables that can be used to identify and match a specific section of

Struggling to apply a regex (with 1 variable) to compare against a HTML code page stored as text. The HTML code is separated into an array, with each element representing a snippet like the one below. Each element showcases details of fictional Houses (na ...

Tips for continuing to write to the same CSV file without starting over

Currently, I am utilizing a nodejs package to write data to a CSV file every minute. The initial creation of the file and the first write operation work fine. However, when attempting to write to the same file again, I encounter an error in nodejs. Despite ...

Creating multiple child processes simultaneously can be achieved by spawning five child processes in parallel

My goal is to simultaneously run five spawn commands. I am passing five hls stream urls to the loop, and these streamlink commands are meant to record the video for 5 seconds before terminating those processes. I have attempted various async methods but a ...

Guide to importing scoped styles into a <NextJS> component

When importing a CSS file, I usually do it like this: import './Login.module.css'; In my component located at components/login/index.js, I define elements with classes such as <div className="authentication-wrapper authentication-basic ...

Steps for wrapping a class with a higher order component

Is it feasible to encapsulate a class component within a higher order component (HOC) that is also a class? import React, { Component } from "react"; import { View } from "react-native"; import { Toast } from "react-native-easy-toast"; const withToast = ...