Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/471.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在nodejs代码中使用res.json()时获取空数组_Javascript_Node.js - Fatal编程技术网

Javascript 在nodejs代码中使用res.json()时获取空数组

Javascript 在nodejs代码中使用res.json()时获取空数组,javascript,node.js,Javascript,Node.js,我有一个代码片段,显示了所有帖子以及帖子的用户名和用户显示图片(DP)。代码片段工作得非常好,日志输出非常好,但是,我无法将其作为JSON对象发布作为响应 这是我的代码: router.get('/get_posts',jwt, function(req,res){ var send = []; post.find({}).populate({ path:'author', model: User }).exec(function(err,res){ var send

我有一个代码片段,显示了所有帖子以及帖子的用户名和用户显示图片(DP)。代码片段工作得非常好,日志输出非常好,但是,我无法将其作为JSON对象发布作为响应

这是我的代码:

router.get('/get_posts',jwt, function(req,res){
    var send = [];

    post.find({}).populate({ path:'author', model: User }).exec(function(err,res){
    var send = [];
    if(err)
    {console.log(err,err.stack);}
    if(res){
          for(var i=0;i<res.length;i++)
    {
     send.push({username:res[i].author.username,dp:res[i].author.dp,subject:res[i].subject,Body:res[i].Body,nofppl:res[i].nofppl});
    }
    console.log(send);
      }
    });
    res.json(send);
    });
(我刚刚发布了3次相同的帖子,代码片段有效,我不想在回复中发布这些数据)

我得到一个空数组作为响应

答复如下:

[]

所以,在将所有对象推入数组之前,会抛出某种数组,我该怎么办?

您有一个异步调用
post.find({})

因此,在构建json的异步回调之前调用
res.json(send)
。这就是结果为空的原因


您应该在exec函数中执行
res.json(xxx)
发送变量可能有问题,您从范围外返回json,并且您还有一个额外的右括号。仔细检查你的代码

post.find({}).populate({ path: 'author', model: User }).exec(function (err, res) {
    var data = [];
    if (err) { console.log(err, err.stack); }
    if (res) {
        for (var i = 0; i < res.length; i++) {
         data.push({ username: res[i].author.username, dp: res[i].author.dp, subject: res[i].subject, Body: res[i].Body, nofppl: res[i].nofppl });
        }
        res.json(data);
        return;
    }
});
post.find({}).populate({path:'author',model:User}).exec(函数(err,res){
var数据=[];
if(err){console.log(err,err.stack);}
如果(res){
对于(变量i=0;i
试试下面给出的代码,它应该可以工作,java脚本中的send is关键字使用其他键创建数组,然后调用全局send数组,这就是为什么你会变空

 router.get('/get_posts',jwt, function(req,res){
 var array = [];
  post.find({}).populate({ path:'author', model: User }).exec(function(err,result){
   if(err)
    {
     console.log(err,err.stack);
    }
 if(result){
  for(var i=0;i<result.length;i++)
    {
    array.push({
        username:result[i].author.username,
        dp:result[i].author.dp,
        subject:result[i].subject,
        Body:result[i].Body,
        nofppl:result[i].nofppl
    });
      }
    console.log(array);
    res.json(array);
     }
  });
  });
router.get('/get_posts',jwt,function(req,res){
var数组=[];
post.find({}).populate({path:'author',model:User}).exec(函数(err,result){
如果(错误)
{
日志(err,err.stack);
}
如果(结果){

对于(var i=0;i而言,这有时可能由混合
res
变量名引起:

router.get('/get_posts',jwt, function(req,res){
  getData().then(res => {
     res.json({ res });
  })
});

现在,我在控制台和HTTP结果中都得到了“[]”,如果我在exec()中写入“res.json(send)”,我认为因为它是异步的,所以它执行的任何操作都需要较少的时间。仍然没有得到我想要的。
router.get('/get_posts',jwt, function(req,res){
  getData().then(res => {
     res.json({ res });
  })
});