无法使用Node.js从azure函数应用程序连接到azure cosmos db

时间:2019-09-21 07:38:59

标签: node.js azure-functions azure-cosmosdb azure-iot-hub msdn

我正在尝试从cosmos DB中的IoT发送传入的数据。 我正在功能应用程序中接收数据,但是很遗憾,我无法使用在cosmos DB快速入门和连接字符串刀片中提供的连接字符串来连接cosmos DB。

下面是我的node.js应用程序:

module.exports = function (context, IoTHubMessage) {
  try {
    var dbName = "temp-db";
    var collectionName = "messages";

    context.log(`JavaScript eventhub trigger function called for message array: ${IoTHubMessage}`);
    context.log(`datatype of message: ${typeof IoTHubMessage}`);
    var json_message = JSON.stringify(IoTHubMessage);
    context.log(`json message: ${json_message}`);

    var mongoClient = require("mongodb").MongoClient;
    context.log('MongoClient created');
    const pass = "Q********************XTB988aNw4CecjmZsSqTpCeXkjlzCrmljzjq58T9AqeuVvSJrUPBpc4rBSSD1CQ=="    
    const encodedpass = encodeURIComponent(pass)
    const connectionString= `mongodb://iot-db:${pass}@iot-db.documents.azure.com:10255/?ssl=true`

    context.log(`connection string:\n ${connectionString}`)
    mongoClient.connect(connectionString, function (err,client){
    context.log('check0...');
    if(err){
        context.log(`Error occurred while connecting to DB ${err}`)
    } else{
        context.log('MongoClient connected to DB');
    }
      context.log('check1...');

      var collection = client.db(dbName).collection(collectionName);
      context.log('MongoClient collection retreived');
      collection.insertOne(IoTHubMessage, {w: 1});
      //collection.insertOne({"testKey": 13.56}, {w: 1});
      client.close();
      context.log(`Saved message: ${IoTHubMessage}`);
      context.done();
    });

    context.log('check2...');
  } catch (e){
    context.log(`Error ${e}`);
  }

  context.log('Done called');
  context.done();
};

这是我面临的问题:

  1. 上面的应用程序没有给出任何错误,但是在运行回调函数时未得到执行。(我知道这是因为在回调函数中未发生打印。)
  2. 我试图传递非URI编码的密码。然后进入回调函数,但抱怨密码包含非法字符。
  3. 在传递编码后的密码时,它不会进入回调函数。
  4. 如何检查当前正在使用的节点驱动程序?
  5. 如何检查10255端口是否打开?

我正在附上node.js应用的屏幕截图

screenshot of the code on portal

0 个答案:

没有答案