MongoDB Find函数不返回,即使存在

时间:2019-03-01 06:36:35

标签: javascript mongodb

我认为此find函数的语法可能不正确,因为即使不存在文档,它仍会说有一个'docs',但是如果文档存在,则应该作为记录的'docs'只会返回空?

功能:

  addRefund : (refundCalc, callback) => {
    order_number = refundCalc.refundDetails.customer_details.order.order_number;
    console.log('TYPE OF ORDER NUMBER' + typeof order_number)
    dbconnect.createConnection()
    refund.find({order_number: order_number}, (err, docs) => {
        if (docs) {
            console.log('docss!!!!!!!!!!!!!' + docs[0])
            console.log('calling within error!!!!!!')
            let notStored = {"refundDocStored" : "False"}
            callback(notStored)
            dbconnect.closeConnection();
        }

然后是我的架构,这是我可能出错的地方,也许是它无法正确查找我不知道的订单号-

{
"_id": {
    "$oid": "5c7835dabffecf7f4be8ae69"
},
"refundDetails": {
    "refund": {
        "shipping": {
            "amount": 10,
            "tax": 0,
            "maximum_refundable": 10
        },
        "refund_line_items": [
            {
                "quantity": 1,
                "line_item_id": 1835202838591,
                "location_id": null,
                "restock_type": "no_restock",
                "price": "15.44",
                "subtotal": "15.44",
                "total_tax": "0.00",
                "discounted_price": "15.44",
                "discounted_total_price": "15.44",
                "total_cart_discount_amount": "0.00"
            }
        ],
        "transactions": []
    },
    "customer_details": [
        {
            "order": {
                "customer_name": "Dale grant",
                "customer_email": "dalebradleygrant@yahoo.co.uk",
                "order_id": 791421321279,
                "order_number": 1025,
                "order_date": "2019-02-28T14:21:59-05:00",
                "total_price": "25.44",
                "subtotal_price": "15.44",
                "shipping_price": "10.00",
                "total_tax": "0.00",
                "items": [
                    {
                        "id": 1835202838591,
                        "variant_id": 7051095769151,
                        "title": "Womens Summer Black Camis Dress",
                        "name": "Womens Summer Black Camis Dress - Black / S",
                        "quantity": 1,
                        "price": "15.44",
                        "product_id": 561553309759
                    }
                ],
                "image_links": [
                    {
                        "product_id": 561553309759,
                        "image_src": "https://cdn.shopify.com/s/files/1/0013/6688/4415/products/product-image-389399624.jpg?v=1521568884",
                        "varrient_id": []
                    },
                    {
                        "product_id": 561553309759,
                        "image_src": "https://cdn.shopify.com/s/files/1/0013/6688/4415/products/product-image-389399629.jpg?v=1521568888",
                        "varrient_id": [
                            7051095769151,
                            7051095834687,
                            7051095867455,
                            7051095900223
                        ]
                    }
                ],
                "fullfillmentText": "Hi, Please find your order details below:"
            }
        }
    ]
},
"domain": "chatbotdemo.myshopify.com",
"__v": 0

}

任何帮助都将是惊人的..真的卡在这一个了!

谢谢。

2 个答案:

答案 0 :(得分:0)

尝试与toArray函数一起使用

addRefund : (refundCalc, callback) => {
    order_number = refundCalc.refundDetails.customer_details.order.order_number;
    console.log('TYPE OF ORDER NUMBER' + typeof order_number)
    dbconnect.createConnection()
    refund.find({order_number: order_number}).toArray(function(err, docs) {
        if (docs) {
            console.log('docss!!!!!!!!!!!!!' + docs[0])
            console.log('calling within error!!!!!!')
            let notStored = {"refundDocStored" : "False"}
            callback(notStored)
            dbconnect.closeConnection();
        }})}

答案 1 :(得分:0)

尝试使用点表示法访问嵌入式数组

addRefund : (refundCalc, callback) => {
order_number = refundCalc.refundDetails.customer_details.order.order_number;
console.log('TYPE OF ORDER NUMBER' + typeof order_number)
dbconnect.createConnection()
refund.find({customer_details.order.order_number: order_number}, (err, docs) => {
    if (docs) {
        console.log('docss!!!!!!!!!!!!!' + docs[0])
        console.log('calling within error!!!!!!')
        let notStored = {"refundDocStored" : "False"}
        callback(notStored)
        dbconnect.closeConnection();
    }