Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.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
Arrays mongoose find返回了未定义的文档_Arrays_Mongodb_Mongoose_Mongodb Query - Fatal编程技术网

Arrays mongoose find返回了未定义的文档

Arrays mongoose find返回了未定义的文档,arrays,mongodb,mongoose,mongodb-query,Arrays,Mongodb,Mongoose,Mongodb Query,我有一些代码看起来像这样。从字面上看,这只是表面上的改变 &从我的实际代码中删除: Entry.find(conditions) .select('ymd intentions outcomes goalCounts') .lean() .exec() .then(function (entries) { if (!entries.length) { res.write(']') res.end() } entries[entries.length-1].isLast

我有一些代码看起来像这样。从字面上看,这只是表面上的改变 &从我的实际代码中删除:

Entry.find(conditions)
.select('ymd intentions outcomes goalCounts')
.lean()
.exec()
.then(function (entries) {
  if (!entries.length) {
    res.write(']')
    res.end()
  }
  entries[entries.length-1].isLast = true
  // more processing of entries etc
}, function (err) {
  // do something with error
})
它通常可以工作,但我的生产服务器刚刚报告了一个错误:isLast=true行失败

TypeError:无法设置未定义的属性“isLast”

因为有一个检查确保数组本身不是空的,所以我知道传入这个函数的是一个非空数组,它的最后一项本身没有定义。ymd是一个必填字段,因此不应该有任何{}结果


在任何情况下,这种预期行为是否与精益有关?还是它代表了某种mongo bug?

您的代码在您提到的if检查之后继续,因此如果条目长度为0,您将看到错误

在res.end调用后返回,或将引发错误的行放入else子句中:


哈当然看到res.end让我觉得它已经完成了。哼!我比这更清楚。
if (!entries.length) {
  res.write(']')
  res.end()
  return;
}
entries[entries.length-1].isLast = true
if (!entries.length) {
  res.write(']')
  res.end()
} else {
  entries[entries.length-1].isLast = true
}