具有Mongoose数据库的Node.js发现问题

时间:2017-04-22 06:30:44

标签: node.js mongodb mongoose

我一直有一个非常奇怪的问题,我认为这可能是我在mongoose模式中进行嵌套级别的方式。首先让我们得到架构:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var RLSchema = new Schema({
    user_id: { type: String, required: true },
    coordinate: {
        accuracy: Number,
        latitude: Number,
        longitude: Number
    },
    agency_id: String,
    received_time: { type: Date, default: Date.now },
    department_id: String
});

module.exports = mongoose.model('RL', RLSchema);

我正在创建一个动态搜索,因为我不知道他们将要搜索什么。我所知道的是,如果您根据收到的时间进行搜索,则需要发送收件人来自:

var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var regexp = require('node-regexp');

var RL = require('./app/models/rl');

var uristring = process.env.MONGODB_URI || 'mongodb://localhost/mydev';

mongoose.connect(uristring, function (error) {
    if (error) {
        console.log(error);
    }
});

app.get('/api/rl', function(req, res) {
    var findQuery = "{";
    var qry = "";

    if (req.query.agency_id) {
        findQuery = findQuery + " agency_id: \"" + req.query.agency_id + "\",";
    }

    if (req.query.department_id) {
        findQuery = findQuery + " \"department_id\": \"" + req.query.department_id + "\",";
    }

    if (req.query.user_id) {
        findQuery = findQuery + " \"user_id\": \"" + req.query.user_id + "\",";
    }

    var isOne = ((req.query.from) || (req.query.to));
    var isBoth = ((req.query.from) && (req.query.to));

    if (isOne) {
        if (isBoth) {
            findQuery = findQuery + " \"received_time\": { \"$lte\": " + req.query.to + ", \"$gte\": " + req.query.from + "},"
        } else {
            res.status(400).json("{ message: 'Missing the to or from time for searching}'");
            return res;
        }
    }

    qry = findQuery.substring(0, findQuery.length - 1);
    qry = qry + "}";
    if (qry.length < 2) {
        qry = "{}";
    }

    if (!isBoth) {
        qry = "";
    }

    RL.find(JSON.parse(qry)).toArray(function(err, doc) {
        if (err) {
            handleError(res, err.message, "Failed to get information");
        } else {
            res.status(200).json(doc);
        }
    });
});

每次我打电话给我都会回来: 错误 内部服务器错误

即使我删除所有代码并只是发送下面的内容我仍然会收到内部服务器错误。你知道为什么这不起作用吗?

ResponderLocation.Find({ agency_id: req.query.agency_id }).toArray(function(err, rl){
        if(err)
            res.send(err);

        res.json(rl);
    });

1 个答案:

答案 0 :(得分:1)

正如q4w56指出的那样,你应该构建一个与字符串相对的对象,而没有函数Find

以下是我将如何重构您的代码:

app.get('/api/rl', function(req, res) {
    var query = {};
    if (req.query.agency_id) {
        query.agency_id = req.query.agency_id;
    }
    if (req.query.department_id) {
        query.department_id = req.query.department_id;
    }
    if (req.query.user_id) {
        query.user_id = req.query.user_id;
    }
    if (!req.query.from && !req.query.to) {
        // .json() expects a JSON not a string as argument
        return res.status(400).json({ message: 'Missing the to or from time for searching' });
    }
    query.received_time = {};
    if (req.query.from) {
        query.received_time.$gte = req.query.from;
    }
    if (req.query.to) {
        query.received_time.$lte = req.query.to;
    }
    // no need for toArray()
    ResponderLocation.find(query, function(err, locations) {
        if (err) {
            handleError(res, err.message, "Failed to get information");
        } else {
            // no need to send 200 status as that is default
            res.json(locations); 
        }
    });
});