我正在使用 javascript API 查询 AMPS SOW。我的函数如下所示:
sow_query = async(topic, filter, options) => {
await this.connection
await this.client.sow(this.onMessage, topic, filter, options)
return this.message
}
onMessage = (message)=>{
this.message.push(message.data)
}
//Calling functions
const getData = async(date) =>{
let data = await getAmpsData(date)
}
async getAmpsData(date){
const filter = "some filter"
const data = await ampsClient.sow_query(topic, filter)
data.forEach((element) => console.log(element))
}
看起来我对 ampsClient.sow_query 的调用没有等待回调完成,因此返回一个空列表。因此,我无法遍历调用函数 getAmpsData 中的数据。 我知道它与异步/等待有关,因为如果我只是在 getAmpsData 中执行 console.log(data) 我可以在承诺解决时看到数据(可能在 2 秒后)。无论如何我可以让它工作吗?
答案 0 :(得分:0)
如果我理解正确,public ProductModel GetProductDetail(uint product_id)
{
ProductModel product = null;
using (var productCmd = this.MySqlDatabase.Connection.CreateCommand() as MySqlCommand)
{
productCmd.CommandText = @"SELECT * FROM products WHERE product_id = @product_id";
productCmd.Parameters.Add("@product_id", SqlDbType.int).Value = product_id;
using (var reader = productCmd.ExecuteReader())
{
while (reader.Read())
{
product = new ProductModel()
{
product_id = reader.GetValue<UInt32>("product_id"),
subcategory_id = reader.GetValue<UInt32>("subcategory_id"),
product_name = reader.GetValue<String>("product_name"),
description = reader.GetValue<String>("description"),
is_recent = reader.GetValue<Boolean>("is_recent"),
is_popular = reader.GetValue<Boolean>("is_popular"),
is_available = reader.GetValue<Boolean>("is_available"),
price = reader.GetValue<Decimal>("price"),
overall_rating = reader.GetValue<Double>("overall_rating"),
views = reader.GetValue<UInt32>("views"),
people_rated = reader.GetValue<UInt32>("people_rated"),
date_posted = reader.GetValue<DateTime>("date_posted")
};
}
}
}
if (product == null)
return null;
var imageList = new List<ImagesModel>();
using (var imageCmd = this.MySqlDatabase.Connection.CreateCommand() as MySqlCommand)
{
imageCmd.CommandText = @"SELECT * FROM images WHERE product_id = @product_id";
imageCmd.Parameters.Add("@product_id", SqlDbType.int).Value = product_id;
using (var reader = imageCmd.ExecuteReader())
{
while (reader.Read())
{
imageList.Add(new ImagesModel
{
imageurl = reader.GetValue<String>("imageurl"),
created_date = reader.GetValue<DateTime>("created_date")
});
}
}
}
product.Images = imageList;
return product;
}
中的 data
是预期的数组,但 getAmpsData
中的 data
未定义。这不是异步/等待问题,您只是忘记将 getData
添加到 return data;
。
答案 1 :(得分:0)
我不确定您使用的是什么软件包。但是,也许,它使用回调函数来获取 .sow
函数 - message.data
的结果。
按照您的逻辑,onMessage
函数将在 data.forEach
完成后调用,您可以尝试在 console.log
函数中添加 onMessage
行。
也许,包裹有这样做的重要原因。但是,要解决此问题,您可以将 .sow
函数包装到一个 promise 中。
sow_query = async (topic, filter, options) => {
await this.connection // ???
return new Promise((resolve) => { // wrap in a promise
this.client.sow( // no need to wait .sow
(message) => {
resolve(message.data); // just resolve when we get the message's data
},
topic, filter, options)
});
}
//Calling functions
const getData = async (date) => {
let data = await getAmpsData(date)
}
async getAmpsData(date) {
const filter = "some filter"
const data = await ampsClient.sow_query(topic, filter)
data.forEach((element) => console.log(element))
}