Azure - Node JS 程序未将行插入 Cosmos DB
AzureFN - NodeJS program is not inserting rows into CosmosDB
我在 NodeJS 中有以下 Azure 函数
具有触发器:IoT 中心事件。
我需要将消息传输到 cosmos DB。
module.exports = function (context, IoTHubMessage) {
try {
var dbName = "db";
var collectionName = "encodedmessages";
context.log(`JavaScript eventhub trigger function called for message array: ${IoTHubMessage}`);
var mongoClient = require("mongodb").MongoClient;
context.log('MongoClient created');
mongoClient.connect("mongodb://xxx:password==@xxx.mongo.cosmos.azure.com:10255/?ssl=true&replicaSet=globaldb&retrywrites=false&maxIdleTimeMS=120000&appName=@db@",{useNewUrlParser: true, authSource: dbName}, function (err, client) {
if(err){
context.log(`Error occurred while connecting to DB ${err}`)
} else{
context.log('MongoClient connected to DB');
}
var collection = mongoClient.db(dbName).collection(collectionName);
context.log('MongoClient collection retreived');
collection.insertOne(IoTHubMessage, {w: 1});
//collection.insertOne({"testKey": 13.56}, {w: 1});
mongoClient.close();
context.log(`Saved message: ${IoTHubMessage}`);
context.done();
});
} catch (e){
context.log(`Error ${e}`);
}
context.log('Done called');
context.done();
};
我还有一个控制台应用程序将消息发送到物联网中心 运行,如下所述:
https://docs.microsoft.com/en-us/azure/iot-hub/quickstart-send-telemetry-dotnet
输出如下:
2020-10-30T12:06:41.968 [Information] JavaScript eventhub trigger function called for message array: Test Message
2020-10-30T12:06:41.972 [Information] MongoClient created
2020-10-30T12:06:41.972 [Information] Done called
2020-10-30T12:06:42.026 [Information] Executed 'Functions.ProcessEncodedMessages' (Succeeded, Id=2fcb7fa8-b194-4499-bc39-775aef86aac0, Duration=24606ms)
我真的不明白为什么我在这段代码的日志中看不到消息:
如果(错误){
context.log(Error occurred while connecting to DB ${err}
)
} 别的{
context.log('MongoClient connected to DB');
}
好像它永远不会达到那个点,而且我也没有收到关于连接字符串的任何错误。
我相信 insertOne
函数 returns 是一个承诺,您不会等待它,因此它会转到下一个语句,即 mongoClient.close()
从而关闭连接。
您可以re-factor您的代码使用 ES8 async-await 和 post 解析 insertOne
函数的承诺安排调用以关闭连接。
这是来自官方文档的reference
。
const { MongoClient } = require("mongodb");
module.exports = function (context, IoTHubMessage) {
const dbName = "db";
const collectionName = "encodedmessages";
const connectionString = `mongodb://xxx:password==@xxx.mongo.cosmos.azure.com:10255/?ssl=true&replicaSet=globaldb&retrywrites=false&maxIdleTimeMS=120000&appName=@db@`;
const options = {
useNewUrlParser: true,
authSource: dbName
};
context.log(`JavaScript eventhub trigger function called for message array: ${IoTHubMessage}`);
const client = new MongoClient(connectionString, options);
try {
context.log('MongoClient created');
await client.connect();
const database = client.db(dbName);
const collection = database.collection(collectionName);
context.log('MongoClient collection retreived');
const insertResult = await collection.insertOne(IoTHubMessage, {w: 1});
context.log(`Saved message: ${IoTHubMessage}`, insertResult);
context.done();
context.log('Done called');
} catch (e){
context.log(`Error ${e}`);
context.done();
} finally {
client.close();
}
};
我在 NodeJS 中有以下 Azure 函数 具有触发器:IoT 中心事件。
我需要将消息传输到 cosmos DB。
module.exports = function (context, IoTHubMessage) {
try {
var dbName = "db";
var collectionName = "encodedmessages";
context.log(`JavaScript eventhub trigger function called for message array: ${IoTHubMessage}`);
var mongoClient = require("mongodb").MongoClient;
context.log('MongoClient created');
mongoClient.connect("mongodb://xxx:password==@xxx.mongo.cosmos.azure.com:10255/?ssl=true&replicaSet=globaldb&retrywrites=false&maxIdleTimeMS=120000&appName=@db@",{useNewUrlParser: true, authSource: dbName}, function (err, client) {
if(err){
context.log(`Error occurred while connecting to DB ${err}`)
} else{
context.log('MongoClient connected to DB');
}
var collection = mongoClient.db(dbName).collection(collectionName);
context.log('MongoClient collection retreived');
collection.insertOne(IoTHubMessage, {w: 1});
//collection.insertOne({"testKey": 13.56}, {w: 1});
mongoClient.close();
context.log(`Saved message: ${IoTHubMessage}`);
context.done();
});
} catch (e){
context.log(`Error ${e}`);
}
context.log('Done called');
context.done();
};
我还有一个控制台应用程序将消息发送到物联网中心 运行,如下所述: https://docs.microsoft.com/en-us/azure/iot-hub/quickstart-send-telemetry-dotnet
输出如下:
2020-10-30T12:06:41.968 [Information] JavaScript eventhub trigger function called for message array: Test Message
2020-10-30T12:06:41.972 [Information] MongoClient created
2020-10-30T12:06:41.972 [Information] Done called
2020-10-30T12:06:42.026 [Information] Executed 'Functions.ProcessEncodedMessages' (Succeeded, Id=2fcb7fa8-b194-4499-bc39-775aef86aac0, Duration=24606ms)
我真的不明白为什么我在这段代码的日志中看不到消息:
如果(错误){
context.log(Error occurred while connecting to DB ${err}
)
} 别的{
context.log('MongoClient connected to DB');
}
好像它永远不会达到那个点,而且我也没有收到关于连接字符串的任何错误。
我相信 insertOne
函数 returns 是一个承诺,您不会等待它,因此它会转到下一个语句,即 mongoClient.close()
从而关闭连接。
您可以re-factor您的代码使用 ES8 async-await 和 post 解析 insertOne
函数的承诺安排调用以关闭连接。
这是来自官方文档的reference
。
const { MongoClient } = require("mongodb");
module.exports = function (context, IoTHubMessage) {
const dbName = "db";
const collectionName = "encodedmessages";
const connectionString = `mongodb://xxx:password==@xxx.mongo.cosmos.azure.com:10255/?ssl=true&replicaSet=globaldb&retrywrites=false&maxIdleTimeMS=120000&appName=@db@`;
const options = {
useNewUrlParser: true,
authSource: dbName
};
context.log(`JavaScript eventhub trigger function called for message array: ${IoTHubMessage}`);
const client = new MongoClient(connectionString, options);
try {
context.log('MongoClient created');
await client.connect();
const database = client.db(dbName);
const collection = database.collection(collectionName);
context.log('MongoClient collection retreived');
const insertResult = await collection.insertOne(IoTHubMessage, {w: 1});
context.log(`Saved message: ${IoTHubMessage}`, insertResult);
context.done();
context.log('Done called');
} catch (e){
context.log(`Error ${e}`);
context.done();
} finally {
client.close();
}
};