使用Mongoose查询是正确的,但是forEach之后数组总是空的.Nodejs / MongoDB
Correct query using Mongoose, but array is always empty after forEach. Nodejs/MongoDB
我想在异步方法中的forEach之后返回一个名称数组。for each和res.send都在asynn中。方法。forEach工作得很好,但我总是得到一个空数组。
这是我在网站方面的请求:
function retrieveCharity(){
$.ajax({
type:"GET",
url: "http://xxx.xxx.x.xx:9000/charity",
success: function(data){
data.charities.forEach(function(charityName){
$(".charity-ul").append("<li>"+charityName+"</li>");
})
}
})
}
这是我的路由器。在NodeJs端获取监听器:
router.get('/', function (req, res){
log.d("Entrou no method post - Charity");
teamModel.find({},function(err,charities){
var charityMap = {};
if(err){
console.log("Err in retrieving Charity")
log.d("Err in retrieving")
res.send({
"status":"error"
})
}else{
charities.forEach(function(charity){
charityMap[charity._id]= charity.teamName;
console.log("Print here the name of the charities I have,PLEASE" + charity.teamName);
log.d("foEach - data",charity);
})
res.send({
"charities": charityMap
})
}
})//end of findCahrity
});
所有的日志。d和我要求打印在里面的信息是正确的,并且打印出来了。问题是我返回的数组总是空的,但是(forEach)和res.send都在异步方法中。
我总是在邮局收到这个:
{
charities: { }
}
控制台总是说:
Uncaught TypeError: undefined is not a function
this line:
data.charities.forEach(function(charityName){
有人能帮帮我吗?我做错了什么?提前感谢。
我不认为Object.prototype.forEach
是标准的。data.charities
是一个对象而不是数组。你可以尝试从
var charityMap = {};
与charityMap[charity._id]= charity.teamName;
var charityMap = [];
with charityMap.push(charity.teamName)
或更改客户端的迭代
相关文章:
- 从数组NodeJs中获取对象值
- 如何从NodeJS中的模块返回数组
- 将对象数组转换为与nodejs/pg/unnest兼容的数组
- 使用Express4将数组从Nodejs中的app.js传递到routes/index.js
- 从Nodejs中的函数返回数组,并将其发送到ejs视图
- 通过 nodejs 过滤对象数组
- Typescript,NodeJS,ES6-在数组上查找索引
- 在Nodejs中将数组转换为类似子的对象字符串
- 有效地串行化(并从nodejs读取)int数组
- 在nodejs中执行procces数组时保存数据
- 如何在 NodeJS 服务器中解析 ajax 发送的数组/对象
- 删除在数组中的 NodeJS 中真正意味着什么
- 从 Nodejs socketio 数组获取所有套接字 id
- 如何在 nodejs 中将项目添加到数组中
- 从动态生成的函数数组 nodeJs 异步
- Nodejs 数组未按预期工作
- 从JSON数组nodejs中获取2个值
- 解析JSON数组nodejs
- 将数组对象转换为简单的数组nodejs
- 数组nodejs的字符串