Javascript 查询mongoose以执行连接

Javascript 查询mongoose以执行连接,javascript,node.js,mongodb,express,mongoose,Javascript,Node.js,Mongodb,Express,Mongoose,我有两个收藏设置,如下所示,日期和街道 我想要实现的是,通过参数StreetName查询街道,并查找它的唯一ID,然后通过该ID查询其他集合以提取所有匹配的日期 我的路线设置为/wasteDate/:StreetName。以下是我所拥有的: model.js var DateSchema = new Schema({ date: { type: Date }, street_id: { type: String, } }); var StreetSchem

我有两个收藏设置,如下所示,
日期
街道

我想要实现的是,通过参数
StreetName
查询街道,并查找它的唯一ID,然后通过该ID查询其他集合以提取所有匹配的日期

我的路线设置为
/wasteDate/:StreetName
。以下是我所拥有的:

model.js

var DateSchema = new Schema({
  date: {
    type: Date
  },   
  street_id: {
    type: String,
  }
});

var StreetSchema = new Schema({
  name: {
    type: String
  }
});
routes.js

module.exports = function(app) {
    var wasteCollections = require('../controllers/wasteController'); 
    app.route('/wasteDate/:streetName')
        .get(wasteCollections.get_dates_by_street_name);
}; 
controller.js

var mongoose = require('mongoose'),
  ColDate = mongoose.model('Dates'),
  that = this,
  Street = mongoose.model('Streets');
(……)

目前,我在JSON上得到了一个循环引用错误

我认为我做得不对,我可能需要修改我的模式


感谢任何帮助

我从未使用过它,但我认为猫鼬模型可能会解决您的问题


另一种可能的方法是将第二个查询函数作为第一个查询函数的回调。

我从未使用过它,但我认为mongoose模型可能会解决您的问题

另一种可能的方法是将第二个查询函数作为第一个查询函数的回调。

您可以使用(1)
find
两次或(2)

第一种方法是:

exports.manual_get_dates_by_street = function (id, callback) {
    // you are dealing with asynchronous operations, so you have to wait for the callback
    // to execute before you can get the data
    ColDate.find({ street_id: id }).lean().exec(callback);
};

exports.get_dates_by_street_name = function (req, res) {
    // you are expecting one result, so use findOne instead of find
    Street.findOne({ name: req.params.streetName }, function (err, street) {
        // make sure you handle errors properly such as stopping execution of
        // the next lines or else you may get unexpected errors
        if (err) 
            return res.send(err);

        // we pass a callback that will be executed once results (or an error) are found
        that.manual_get_dates_by_street(street._id, function (err, dates) {
            res.json({ dates: dates });
        });
    });
};
您可以使用(1)
find
两次或(2)

第一种方法是:

exports.manual_get_dates_by_street = function (id, callback) {
    // you are dealing with asynchronous operations, so you have to wait for the callback
    // to execute before you can get the data
    ColDate.find({ street_id: id }).lean().exec(callback);
};

exports.get_dates_by_street_name = function (req, res) {
    // you are expecting one result, so use findOne instead of find
    Street.findOne({ name: req.params.streetName }, function (err, street) {
        // make sure you handle errors properly such as stopping execution of
        // the next lines or else you may get unexpected errors
        if (err) 
            return res.send(err);

        // we pass a callback that will be executed once results (or an error) are found
        that.manual_get_dates_by_street(street._id, function (err, dates) {
            res.json({ dates: dates });
        });
    });
};

谢谢我请客了,谢谢。我请客了,谢谢。关于回调,您是对的,但我已经标记了另一个答案,因为它给出了完整的代码示例。谢谢。关于回调,您是对的,但我已经标记了另一个答案,因为它给出了完整的代码示例。