Mongoose - 无法在find中获取数据

时间:2017-10-24 11:22:44

标签: node.js mongodb api mongoose find

我发送的api请求包含空格。我使用decodeURIComponent删除了空格,但我仍然得到[]作为回应。这是我的代码

    router.get('/products/searchproduct/:name', (req, res, next) => {

    var request = decodeURIComponent(req.params.name)
    console.log('------------------------------------');
    console.log(request);
    console.log('------------------------------------');
    Product.find({
        'category.category_name': decodeURIComponent(req.params.name)
    }, function (err, document) {
        if (err)
            return res.status(500).send("There was a problem getting the category based record .");
        console.log('------------------------------------');
        console.log(decodeURIComponent(req.params.name));
        console.log('------------------------------------');
        res.status(200).send(document);

    })
});

enter image description here

产品架构

    //this file contain content scheme

const mongoose = require('mongoose');

//function to call monggose schema
const ProductSchema = mongoose.Schema({
    product_name: {
        type: String,
        required: true
    },
    product_rating: {
        type: Number,
        required: true
    },
    product_price: {
        type: String,
        required: true
    },
    product_btc: {
        type: String,
        required: true
    },
    product_mode: {
        type: String,
        required: true
    },
    product_date: {
        type: String,
        required: true
    },
    product_hasrate: {
        type: String,
        required: true
    },
    product_powerConsumption: {
        type: String,
        required: true
    },
    product_efficiency: {
        type: String,
        required: true
    },
    product_voltage: {
        type: String,
        required: true
    },
    product_frequency: {
        type: String,
        required: true
    },
    product_shipment: {
        type: String,
        required: true
    },
    product_warranty: {
        type: String,
        required: true
    },
    product_contact: {
        type: String,
        required: true
    },
    category: [mongoose.Schema.Types.Mixed]
})

const Product = module.exports = mongoose.model('Product', ProductSchema);

0 个答案:

没有答案