我正在尝试从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();
};
这是我面临的问题:
我正在附上node.js应用的屏幕截图