Azure table 存储 Node JS 函数 app return 响应为空
Azure table storage Node JS function app return response empty
我正在编写一个 Azure 函数应用程序来查询实体,
我在回调中收到响应,但是当结果主体设置为响应时,它仍然 return 是空的,
我无法 return 实体作为响应,因为进程线程在响应设置之前完成,我不确定如何解决这个问题我已经尝试过 setInterval 但没有成功
下面是代码
module.exports = async function (context, req) {
var storage = require('azure-storage')
var storageClient = storage.createTableService(process.env.connectionString)
var storageTableQuery = storage.TableQuery
var query = new storageTableQuery().top(1).where('Mobile eq \'' + context.req.query.mobile + '\'')
storageClient.queryEntities("Customers", query, null, function (error, result) {
if (error) {
context.res = {
status: 200,
body: error
}
}
else {
context.res = {
status: 200,
body: result.entries,
headers: {
'Content-Type': 'application/json'
}
}
}
});
}
问题请参考以下代码
const azure = require("azure-storage");
module.exports = async function (context, req) {
const tableSvc = azure.createTableService(AZURE_STORAGE_CONNECTION_STRING);
const tableQuery = new azure.TableQuery()
.top(1)
.where("PartitionKey eq ?", "Jack");
try {
const result = await queryEntitiesSegmented(
tableSvc,
"test",
tableQuery,
null
);
context.res = {
status: 200,
body: result.entries,
headers: {
"Content-Type": "application/json",
},
};
} catch (error) {
context.res = {
status: 200,
body: error,
};
}
async function queryEntitiesSegmented(
tableSvc,
table,
tableQuery,
continuationToken
) {
return new Promise((resolve, reject) => {
tableSvc.queryEntities(
table,
tableQuery,
continuationToken,
(error, result) => {
if (error) {
reject(error);
} else {
resolve(result);
}
}
);
});
}
我正在编写一个 Azure 函数应用程序来查询实体, 我在回调中收到响应,但是当结果主体设置为响应时,它仍然 return 是空的,
我无法 return 实体作为响应,因为进程线程在响应设置之前完成,我不确定如何解决这个问题我已经尝试过 setInterval 但没有成功
下面是代码
module.exports = async function (context, req) {
var storage = require('azure-storage')
var storageClient = storage.createTableService(process.env.connectionString)
var storageTableQuery = storage.TableQuery
var query = new storageTableQuery().top(1).where('Mobile eq \'' + context.req.query.mobile + '\'')
storageClient.queryEntities("Customers", query, null, function (error, result) {
if (error) {
context.res = {
status: 200,
body: error
}
}
else {
context.res = {
status: 200,
body: result.entries,
headers: {
'Content-Type': 'application/json'
}
}
}
});
}
问题请参考以下代码
const azure = require("azure-storage");
module.exports = async function (context, req) {
const tableSvc = azure.createTableService(AZURE_STORAGE_CONNECTION_STRING);
const tableQuery = new azure.TableQuery()
.top(1)
.where("PartitionKey eq ?", "Jack");
try {
const result = await queryEntitiesSegmented(
tableSvc,
"test",
tableQuery,
null
);
context.res = {
status: 200,
body: result.entries,
headers: {
"Content-Type": "application/json",
},
};
} catch (error) {
context.res = {
status: 200,
body: error,
};
}
async function queryEntitiesSegmented(
tableSvc,
table,
tableQuery,
continuationToken
) {
return new Promise((resolve, reject) => {
tableSvc.queryEntities(
table,
tableQuery,
continuationToken,
(error, result) => {
if (error) {
reject(error);
} else {
resolve(result);
}
}
);
});
}