Get the child element in order to make a query to the database

I need help with searching MongoDB using findOne (or find). I am having trouble accessing a value from within the record, possibly because it's a child object?

Specifically, I am trying to retrieve the order_number field which returns null. However, when using the domain field, it works successfully!

The order_number can be found within the customer_details object.

Here is the database function I am currently using:

dbconnect.createConnection()
    refund.findOne({domain: domain, order_number: order_number}, (err, docs) => {            
        console.log('database return in addRefund   ' + docs)
   dbconnect.closeConnection();
    })

(Even if the order_number exists, it still returns null)

Here is the object structure:

{
    "_id": {
        "$oid": "5c83d0d5ce02f50019b1b146"
    },
    "refundDetails": {
        // ommitted for brevity
    },
    "customer_details": [
        {
            "order": {
                // ommitted for brevity
            }
        }
    ],
    "domain": "dwdwd.wdwd.com",
    "__v": 0
}

Your assistance on this issue would be highly appreciated. Thank you!

Answer №1

It seems like you may be attempting to access a property incorrectly according to what I understand.

Consider testing out this solution:

refund.findOne({domain: domain, 'customer_details.order.order_number': order_number}, (err, data) => {            
   console.log('Received data from the database in addRefund: ' + data)
})

I hope this resolves your issue!

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

Creating a textured path using Threejs

Having trouble drawing a path in my 3D world, as the line class is not helpful. Can anyone offer assistance? See this image I've updated my question I want to draw a path and fill it with texture. var SUBDIVISIONS = 20; geometry = new THREE.Geo ...

What is the solution to fixing the error "TypeError: Cannot read properties of undefined (reading 'map') in Next.js"?

As a newcomer looking to create a blog using the Notion API, Next.js, and Tailwind CSS, I came across this code snippet on this website. Although the code functions correctly in /post/index.js, I encounter an error when trying to use it in /components/Pos ...

Loading images in advance using jCarousel

I need help with preloading images on a jCarousel that loads a JSON feed and generates necessary HTML. Can anyone suggest a way to accomplish this task efficiently? $(".banner ul").jcarousel({ itemLoadCallback:loadTopBanner, auto: 6, wrap: ...

Execute a PHP function using JavaScript/jQuery with AJAX requests

Hello everyone, I am reaching out for assistance with AJAX as I am still quite new to it. Despite the abundance of examples available, I find it challenging to grasp the concept. Specifically, I need help with my validation of a contact form using JavaScri ...

How can I repeatedly show text using knockout js?

How can I use KnockoutJS to display the text and year within a div loop when selecting a brand and model? Example: Mercedes>C *C-180 *2016 *C-200 *2015 Here is the HTML code: <select data-bind="options: manufacturers, optionsCaption:'Bra ...

Discovering the onClick event of an element in React using the DOM: A step-by-step guide

I have been tasked with implementing analytics events for almost every onclick event on our site. While I am new to analytics, the suggested approach was to create a standardized data set for each event and trigger the analytics event during the onclick a ...

Troubleshooting the CORS problem with 'Access-Control-Allow-Origin' while combining Vue.js for the front-end and Express for the back-end

While attempting to make a call to my API on Jazz using Vue.js and Axios, I encountered the following error: Access to XMLHttpRequest at ' _here' from origin 'http://localhost' has been blocked by CORS policy: Response to preflight ...

What is the process for implementing document.ondrop with Firefox?

I'm experiencing an issue where the document.ondrop function seems to be working in Chrome, but not in Firefox. Here's a link to an example demonstrating the problem: In the example, if you try to drop a file onto the page, it should trigger an ...

What is the name of the undefined JSON object and how can I retrieve its parameters?

After sending a request to a web server, I received the following response: The response, when using JSON.stringify, appears as shown below: undefined{\"access_token\":\"Rhazjww5 ...QUiTMVc\",\"token_type\":\"bearer&bs ...

Navigational menu that slides or follows as you scroll

Wondering if anyone knows of a straightforward jQuery or Javascript method to create a navigation sidebar that smoothly moves with the user as they scroll down a page. A good example can be found here: Any suggestions would be greatly welcome. ...

Issue: The function (0, react__WEBPACK_IMPORTED_MODULE_1__.useActionState) is not recognized as a valid function or its output is not iterable

I found a great example of using useActionState at this source. Currently, I am implementing it in my project with Next.js and TypeScript. app/page.tsx: "use client"; import { useActionState } from "react"; import { createUser } from ...

Restricting or postponing HTTP requests in an AngularJS service

I recently developed a service for my AngularJS app that retrieves data by making $http calls through various methods. However, I encountered an issue where these requests were being triggered every time an HTML element in the view (a product details div) ...

Transform Image on Hover in ReactJS

I am working on a Card Component that includes an image and text. Initially, the image is redImage and the text is black. When hovering over the card, I want the redimage to change to whiteimage and the text color to change to white as well. The content ...

JavaScript post method is failing to work, while it successfully runs in POSTMAN

Attempting to send a message to Android devices through a POST request to Firebase Cloud Messaging. When using POSTMAN, the server responds with a success response 200. However, when attempting the same operation with JavaScript using an AJAX request, I r ...

leveraging axios in reactjs to showcase mongodb collection information

I am currently learning MERN stack and exploring how to fetch data from a MongoDB collection using axios. My goal is to display a list of cities on a website but I'm uncertain about the approach. Do I need to utilize super state? Check out this image ...

Issue with dropdown not toggling open when using focus and focusout events

I am facing an issue with two dropdowns having the same class, referred to as "dropdown" in this scenario. I created a fiddle using jQuery to manipulate these dropdowns: $('.dropdown').focus(function () { //Code to manipulate this dropdown }) ...

Having trouble resolving a component from a component library while utilizing NPM link

My React application is set up with Create React App and a separate component library. I'm currently experimenting with using 'npm link' to test changes in the component library directly on my local machine. To achieve this, I first run &ap ...

Leveraging jQuery's .when and .then methods allows for

I'm attempting to utilize a shared ajax function that is meant to retrieve the value returned from the PHP script. function fetchAjax(url, data, type) { result = 0; $.when( $.ajax({ type: "POST", url: url, data: data, ...

After an Ajax form is completed within the .done function, a separate Ajax call must be

Within the confines of an ajax done function lies a form (It's important to note that it resides inside the .done function) $.ajax({ type: "POST", url: "?select_main", data: {}, }) .done(function(data) { <div>\n\ <form id= ...

Dealing with Array Splicing Issues in Angular

Being fairly new to the world of AngularJS, I suspect that I am just making a simple mistake. My goal is to splice the cardTypes array at the var newCard = cardTypes.shift(); line rather than using .shift() so that I can consider my ng-repeat index. Whil ...