gpt4 book ai didi

javascript - Mongo 返回完整数据,而 Mongoose 不返回相对数据

转载 作者:行者123 更新时间:2023-12-03 06:32:28 25 4
gpt4 key购买 nike

当我使用 mongo 命令提示符获取数据时,它工作正常:

enter image description here

但是当我在 node.js 中使用 mongoose 执行此操作时,我没有获得引用的数据。

这是我的代码:

在模型中:

module.exports.getOrderBysalesOrderId = function(salesOrderId, callback){
Order.find({salesOrderId: salesOrderId}).exec(function(err, order){
if(err) return callback(err, null);
callback(null, order);
});
};

在 route :

router.get('/:salesOrderId', function(req, res, next){

var salesOrderId = req.param('salesOrderId');

console.log('order with 1 param is called.');

Order.getOrderBysalesOrderId(salesOrderId, function(err, order) {
if (err) {
return console.error(err);
} else {
//respond to both HTML and JSON. JSON responses require 'Accept: application/json;' in the Request Header
res.format({

//response in dust or jade files
html: function(){
res.render('Order', {
title: 'Order',
"order" : order
});
},

//JSON response will show all orders in JSON format
json: function(){
res.json(order);
}
});
}
});
});

然后使用 Ajax,我尝试获取数据并将其打印到控制台:

$.ajax(
{
type: "GET",
url: "/Order/" + orderNo,
dataType:"json",
async: false,
beforeSend: function(jqXHR, settings)
{
console.log(settings.url);
},
success: function(order)
{
console.log(order);
templateData = order;
},
error: function(xhr, textStatus, errorThrown)
{
alert('ajax loading error... ... '+ url + query);
}
});

控制台输出:

enter image description here

更新:

这是我的模型的结构:

var mongoose = require('mongoose');

var orderSchema = new mongoose.Schema({
salesOrderId: String,
orderDate: Date,
party: {type: mongoose.Schema.Types.ObjectId, ref:'Party'},
items:[{type: mongoose.Schema.Types.ObjectId, ref:'Item'}, {quantity: Number}, {rate: Number}],
dispatches:{
invoiceId: Number,
dispatchDate: Date,
items: [{type: mongoose.Schema.Types.ObjectId, ref:'Item'}, {quantity: Number}, {rate: Number}]
}
});

更新2:

当我尝试使用 populate() 获取数据时,这是我的查询:

module.exports.getOrderBysalesOrderId = function(salesOrderId, callback){
Order.find({salesOrderId: salesOrderId}).populate("items").exec(function(err, order){
console.log(order);
if(err) return callback(err, null);
callback(null, order);
});
};

这里我得到错误:

enter image description here

更新3:

现在我在模型中尝试了下面提到的代码:

module.exports.getOrderBysalesOrderId = function(salesOrderId, callback){
salesOrderId = mongoose.Types.ObjectId(salesOrderId);
Order.find({salesOrderId: salesOrderId}).populate("party").exec(function(err, order){
console.log(order);
if(err) return callback(err, null);
callback(null, order);
});
};

这是我的 db.js 文件:

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/RateDifferenceDB')

现在,我收到另一个错误:

enter image description here

我还在浏览器控制台中收到 url 错误:

enter image description here

最佳答案

我应该使用精益函数来获得结果:

module.exports.getOrderBysalesOrderId = function(salesOrderId, callback){
Order.find({salesOrderId: salesOrderId}).lean().exec(function(err, order){
if(err) return callback(err, null);
callback(null, order);
});
};

关于javascript - Mongo 返回完整数据,而 Mongoose 不返回相对数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38389912/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com