Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js nodejs:无法从主函数中的回调函数读取响应_Node.js_Azure Cosmosdb - Fatal编程技术网

Node.js nodejs:无法从主函数中的回调函数读取响应

Node.js nodejs:无法从主函数中的回调函数读取响应,node.js,azure-cosmosdb,Node.js,Azure Cosmosdb,Noob在nodejs这里,还在努力学习nodejs是如何工作的。请告诉我如何将“getDatafromCosmosDB”函数的回调响应传递到主函数中的变量中,并打印这些值 当我尝试将getDatafromCosmosDB分配给变量“respdata”并尝试打印它时,它不起作用 async function main(params, callback) { const logger = Core.Logger('main2', { level: params.LOG_LEVEL || '

Noob在nodejs这里,还在努力学习nodejs是如何工作的。请告诉我如何将“getDatafromCosmosDB”函数的回调响应传递到主函数中的变量中,并打印这些值

当我尝试将getDatafromCosmosDB分配给变量“respdata”并尝试打印它时,它不起作用

async function main(params, callback) {
    const logger = Core.Logger('main2', { level: params.LOG_LEVEL || 'info' })
    try {
        logger.info('main action')
        const respdata = getDatafromCosmosDB(function(response){
            console.debug(response)
            return response
        });
         console.debug(respdata)
    } catch (e) {
        console.debug(e)
    }
}

exports.main = main
async function getDatafromCosmosDB(callback){
    var query = new azure.TableQuery()
    .top(5)
    tableService.queryEntities('myCosmosTable', query, null, function (error, result, response) {
        if (!error) {
            console.log('success')
            return callback(result.entries)
        }
    });
}

试试这样的

import {
  createTableService,
  services,
  ServiceResponse,
  TableQuery
} from 'azure-storage';

getDatafromCosmosDB(): Promise<ServiceResponse> {
 return new Promise(async (resolve, reject) => {
       this.tableService.queryEntities(
        this.tableName,
        query,
        null,
        (error, _, response) => {
          if (!error) {
            resolve(response);
          } else {
            reject(error);
          }
        }
      );
    });
}

这回答了你的问题吗?这很有帮助,谢谢。我也在寻找一些例子以及下面的答案帮助以及!
this.getDatafromCosmosDB().then(data => {
  console.log(data);
}