【发布时间】:2018-04-09 20:06:23
【问题描述】:
我有一个方法在返回对象数组时失败。如标题中所述 - 该数组已确认已填充,但在响应中为空。
这是完整的流程:
网址:
http://localhost:53000/api/v1/landmarks?lat=40.76959&lng=-73.95136&radius=160
被路由到对应的索引:
api.route('/api/v1/landmarks').get(Landmark.list);
索引调用服务:
exports.list = (req, res) => {
const landmark = new LandmarkService();
landmark.getLandmarks(req)
.then(landmarks => {
var response = new Object();
response.startindex = req.query.page;
response.limit = req.query.per_page;
response.landmarks = landmarks;
res.json(response);
})
.catch(err => {
logger.error(err);
res.status(422).send(err.errors);
});
};
服务方法使用数据访问类返回承诺
getLandmarks(req) {
const params = req.params || {};
const query = req.query || {};
const page = parseInt(query.page, 10) || 1;
const perPage = parseInt(query.per_page, 10);
const userLatitude = parseFloat(query.lat);
const userLongitude = parseFloat(query.lng);
const userRadius = parseFloat(query.radius) || 10;
const utils = new Utils();
const data = new DataService();
const landmarkProperties = ['key','building','street','category','closing',
'email','name','opening','phone','postal','timestamp','type','web'];
return data.db_GetAllByLocation(landmarksRef, landmarkLocationsRef,
landmarkProperties, userLatitude, userLongitude, userRadius);
} // getLandmarks
但是,响应总是空的。
我正在被调用的方法中构建一个数组并用 JSON 对象填充它。这就是应该在响应中发回的内容。在点击返回语句之前,我可以确认属性数组已正确填充。我可以将它记录到控制台。我还可以成功发回一个充满存根值的测试数组。
我有一种感觉,这就是我在 Promise 中的设置方式?
应该返回对象数组的数据访问方法:
db_GetAllByLocation(ref, ref_locations, properties, user_latitude, user_longitude, user_radius)
{
const landmarkGeoFire = new GeoFire(ref_locations);
var geoQuery = landmarkGeoFire.query({
center: [user_latitude, user_longitude],
radius: user_radius
});
var locations = [];
var onKeyEnteredRegistration = geoQuery.on("key_entered", function (key, coordinates, distance) {
var location = {};
location.key = key;
location.latitude = coordinates[0];
location.longitude = coordinates[1];
location.distance = distance;
locations.push(location);
});
var attributes = [];
var onReadyRegistration = geoQuery.on("ready", function() {
ref.on('value', function (refsSnap) {
refsSnap.forEach((refSnap) => {
var list = refSnap;
locations.forEach(function(locationSnap)
{
//console.log(refSnap.key, '==', locationSnap.key);
if (refSnap.key == locationSnap.key)
{
var attribute = {};
for(var i=0; i<=properties.length-1; i++)
{
if(properties[i] == 'key') {
attribute[properties[i]] = refSnap.key;
continue;
}
attribute[properties[i]] = list.child(properties[i]).val();
}
attribute['latitude'] = locationSnap.latitude;
attribute['longitude'] = locationSnap.longitude;
attribute['distance'] = locationSnap.distance;
attributes.push(attribute);
} // refSnap.key == locationSnap.key
}); // locations.forEach
}); // refsSnap.forEach
return Promise.resolve(attributes); <-- does not resolve (throws 'cannot read property .then')
//geoQuery.cancel();
}); // ref.on
}); // onreadyregistration
return Promise.resolve(attributes); <-- comes back empty
}
【问题讨论】:
-
我在你的代码中看不到任何承诺。
-
是的,我把周围的代码用于调用,因为我没有发现它相关,但是 - 你永远不知道。我已根据您的建议添加了它。
-
似乎更新的代码没有发布,现在我要回家了。我回家后会发帖。但是,我可以确认调用代码包含在一个 Promise 中。
-
除了看到promise之外,以后最好把console.log语句全部去掉,让理解起来更快
-
看来我可能需要兑现我的承诺——像这样:javascript.info/promise-chaining。
标签: javascript node.js associative-array