[$promise:承诺,$resolved:错误]

[$promise: Promise, $resolved: false]

本文关键字:错误 resolved 承诺 promise      更新时间:2023-09-26

对angular来说是全新的。

我正在使用$resource从Mongodb获取预约列表。当资源返回时,我得到[$promise:promise,$resolved:false],因此当我执行以下操作时。

当在控制台中挖掘[$promise:promise,$resolved:false]时,我看到了所有的约会。

在角度视图中显示约会之前,我需要进行一些业务规则检查。

我试着用了,但没有改变。$resolved:false是什么意思?

感谢提前提供的帮助

var appointments = Appointments.findByCat({
    catId: $stateParams.catId
  });
  console.log(appointments); //This prints [$promise: Promise, $resolved: false]
   return appointments.length; // is 0 always

$resolved:false是什么意思?

这意味着承诺没有得到解决。换句话说,Appointments.findByCat是异步的,并且还没有完成对值的检索。您需要使用then

function doStuff() {
  return Appointments.findByCat({
    catId: $stateParams.catId
  }).then(function(appointments) {
    console.log(appointments);
    return appointments.length;
  });
}

问题是,调用它的代码也必须具有promise意识。不能只直接使用函数(doStuff)的返回值。您还需要在调用者中使用then

// Won't work
var count = doStuff();
// Use count
// Will work
doStuff().then(function(count) {
   // Use count
});