gpt4 book ai didi

javascript - 确认填充的对象数组返回空

转载 作者:太空宇宙 更新时间:2023-11-04 01:50:51 25 4
gpt4 key购买 nike

我有一个方法在返回对象数组时失败。正如标题中提到的 - 数组已确认已填充,但响应中为空。

以下是完整流程:

网址:

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);
});
};

服务方法使用数据访问类返回 Promise

  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 对象填充它。这就是应该在响应中发回的内容。在我点击 return 语句之前,我可以确认属性数组已正确填充。我可以将其记录到控制台。我还可以成功发回充满 stub 值的测试数组。

我有一种感觉,这就是我在 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
}

最佳答案

看来data.db_GetAllByLocation是一个异步函数,因此调用resolve(landmarks);在异步函数执行完成之前被调用。如果 data.db_GetAllByLocation 返回一个 Promise,则调用 Promise 内的resolve(landmarks)。

data.db_GetAllByLocation().then(function() {
resolve();
})

另请尝试以下修改后的 db_GetAllByLocation()

db_GetAllByLocation(ref, ref_locations, properties, user_latitude, user_longitude, user_radius)
{
return new Promise(function(resolve, reject){
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)
{
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 JSON.stringify(attributes);
return resolve(attributes);
}); // ref.on


}); // onreadyregistration

});

}

关于javascript - 确认填充的对象数组返回空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49740871/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com