modified a file using express framework

I am attempting to utilize mongoDB in order to update the status of an existing document. Despite my backend successfully receiving the routes, the mongoDB update process is not functioning as expected.

router.post('/orders_drivers', function (req, res, next) {
    console.log(req.body);
    Order.update({_id:objectId(req.body.id)}, {$set: {driver:req.body.driver, driverReq:false}}).then(function(order) {
        console.log('UPDATE new driver');
    }).catch(next)
});

Upon logging req.body, I can confirm that the received ID and new $set parameters are accurate. However, the command fails to execute. Any suggestions? It is also concerning that no errors are being generated.

Mongo version being used is v4.0.2

All other routes are functioning correctly in my application.

Answer №1

Issue with version is not the problem here. The error occurs because you are trying to call a function on a non-promiseable value.

To fix this, make sure to include a callback function inside the update.

const mongoose = require('mongoose');

router.post('/orders_drivers', function (req, res, next) {
    console.log(req.body);
    Order.update({
                  _id: mongoose.Types.ObjectId(req.body.id)
                 },
                 {
                  $set: {
                     driver:req.body.driver, driverReq:false
                  }
                 }, 
                 { new: true }, // If you want to return updated order
                 function (err, updatedOrder) { 
                    if (err) throw err;
                    console.log('UPDATE new driver', updatedOrder);
                })
}); 

If req.body.id is already in mongoose ObjectId format, there is no need to convert it again.

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 Within Array Elements in MongoDB: A Comprehensive Guide

While this question may be simple for experienced Mongo users, I have been struggling to find the answer. Here is an example of the type of documents in my collection: { _id:"2a1fd96c-73c5-49e1-a8ca-bd03a20c0197", timestamp:1519725979178, storeID:"x ...

Using JavaScript to pre-select a radio button without any user interaction

Is there a way to programmatically set a radio button in a group without physically clicking on the button? I am attempting to open a jQuery page and depending on a stored value, the corresponding radio button should be selected. I have researched similar ...

Alert: Attempting to invoke the setState method on an unmounted component

While running unit tests with Jest, I encountered a warning when attempting to change the setState input value. The warning message says: "Can't call setState on a component that is not yet mounted. This is a no-op, but it might indicate a bug in you ...

What causes the Object expected error in jQuery?

Looking for a way to demo horizontal collapse pane with a simple web page that includes html, css, and jquery. <html> <head> <script type="text/javascript" src="//code.jquery.com/jquery-1.10.1.js"></script> <title>Sa ...

Calculate the total amount from the selected items on the list, depending on the clicked ('active') element

My main objective is to achieve the following: Before any clicks || After the user selects the desired item After conducting some research, I successfully implemented this using vue.js https://jsfiddle.net/Hanstopz/Lcnxtg51/10/ However, I encountered ...

Having issues with an Angular reactive form that includes a custom form-level validator and the 'blur' updateOn option?

Having issues combining the following: angular reactive form custom validator at form level (cross-field validator) usage of the 'updateOn' option set to 'blur' A demonstration of the problem can be found in this simple stackblitz: h ...

How to transfer data from JavaScript to PHP using AJAX

After spending countless hours attempting to make this function properly, I have come to you for assistance :) I have created a PHP page that can exhibit files from a server. I am able to modify the files using an editor plugin, where the Textarea tag is ...

What is the best way to update parse-server to the latest version?

Currently, I have multiple working apps that are running parse-server on Heroku and utilizing mLab mongoDB. Now, I am looking to update parse-server to a newer version. Can anyone suggest the most efficient and recommended method to achieve this upgrade? ...

Is there a way to transmit React code using Express?

Currently, I'm in the process of creating a coloring demonstration. Initially, I had an SVG file at hand, but I made the decision to utilize svgr for its conversion into a React component. This strategy will offer me the flexibility to easily modify i ...

Show or hide the expand/collapse button based on the height of the container

Looking for a way to hide content in a Div if it's taller than 68px and display an expand option? The challenge lies in detecting the height of the responsive Div, especially since character count varies. I attempted using PHP to count characters bu ...

Mongoose is having trouble connecting, but there are no errors appearing in the console

I am facing an issue with connecting my app.js to MongoDB in a new project. It was working fine previously, but now it won't connect and I don't see any console log or error message. I have double-checked the username and password multiple times. ...

Turn off a feature

I'm having trouble disabling a tooltip that is being utilized from this specific website. The code for setting and calling the tooltip looks like this: this.tooltip = function(){....} $(document).ready(function(){ tooltip(); }); I've attempte ...

The keyup event fails to trigger for the search input in datatables when ajax is being used

When loading a page with a jQuery datatable via AJAX, I am aiming to implement custom filtering when the user starts typing in the default filter provided by datatables. The custom logic needs to be applied when the keyup event is triggered. Since AJAX is ...

What are some effective ways to slow down the image transitions in a Javascript slideshow?

I am currently developing a slideshow that updates Images, Title, and Description simultaneously based on their Array index. The slideshow is functional BUT, my goal is to achieve a smooth transition to the next/previous Image (... title & descript ...

Does the triple equal operator in JavaScript first compare the type of the value?

When using the triple equal operator, it not only measures the value but also the type. I am curious about the order in which it compares the value and returns false if they do not match, or vice versa. ...

Crop multiple images using dropzone.js before uploading

Currently, I have integrated Bootstrap with dropzone.js to allow users to upload images via drag & drop. Everything is functioning smoothly, even with the ability to upload multiple images. However, my next goal is to implement image cropping before uplo ...

URL rewriting dilemma solved

I'm attempting to change any query parameter of '?locale=en' to '/' without redirecting. I placed this code in app.js before the router, but it isn't working as expected – the browser link remains the same. var locale = req. ...

Counting the number of matches in an array field in MongoDB using the $in operator

Let's say I have the following documents: {field: ['a', 'b', 'c', 'd', 'e']} And a request query like this: collection.find({field: {$in: ['x', 'y', 'z', 'a', ...

Can sweetalert2 be used as a tooltip?

I have a query, is it feasible to include a tooltip in the alert message? Alternatively, could there be another tooltip option available? Swal.fire({ title: '<strong>An example with HTML tags</strong>', icon: 'info', ...

Retrieve a zip file using React and Node from a RESTful API

I have an application built with React and Node where I have a table that includes a download button. Clicking the download button triggers a request to my Node app, which in turn should call a REST API to download a zip file directly into the browser. In ...