调用mongodb的azure函数(节点)成功完成但没有错误,但查询没有返回任何结果 [英] azure function (node) calling mongodb finishes without error but nothing comes back from query

查看:75
本文介绍了调用mongodb的azure函数(节点)成功完成但没有错误,但查询没有返回任何结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个节点函数,该函数调用mongo来请求一些数据.没有错误,但似乎也未连接到mongo并获取数据.有什么我做不对的事情吗?

I have a node function that calls mongo to request some data. There is no error but it does not seem to connect to mongo and get the data either. Is there something I'm not doing right?

const MongoClient = require('mongodb').MongoClient;
module.exports = async (context, req) => {

try {
    let connectionString = 'mongodb://<username>:<password>@<endpoint>.documents.azure.com:10255/?ssl=true';
    context.log('******************** 11 *************************');
    MongoClient.connect(connectionString, {uri_decode_auth: true}, function(err, client) {
        if (err) {
            context.log('Failed to connect');
            context.res = { status: 500, body: err.message }
            return context.done();
        }
        context.log('******************** 22 *************************');
        client.db("stuff").collection("items").find({}).toArray(function(err, result) {
            if (err) {
                context.log('Error running query');
                context.res = { status: 500, body: err.message }
                return context.done();
            }
            context.log(result);
            context.log('******************** 44 *************************');
            let message = "Hello " + req.query.name + ". Have a nice day!.  Really...";
            context.res = {
                status: 200,
                headers: { 'Content-Type': 'application/json' },
                body: {"message": message}
            };
            context.done();
            client.close();
        });
        });
    }
    catch(error) {
        context.log('caught the error');
        context.log(error)
    }
};

我在天蓝色日志中看到的输出是:

The output I see in azure logs is:

2018-10-26T12:38:07  Welcome, you are now connected to log-streaming service.
2018-10-26T12:38:17.218 [Information] Executing 'Functions.zap-search' (Reason='This function was programmatically called via the host APIs.', 
Id=e294fde0-d249-4237-a421-e85bde40f843)
2018-10-26T12:38:17.715 [Information] ******************** 11 *************************
2018-10-26T12:38:18.461 [Information] Executed 'Functions.zap-search' (Succeeded, Id=e294fde0-d249-4237-a421-e85bde40f843)

推荐答案

在使用V2的Azure函数时,您需要确定是否要使用Promise或回调.您发布的代码是基于回调的,但由于使用了async关键字,因此已作为承诺导出,并且由于未等待MongoClient.connect,因此运行时在执行这些结果之前会退出您的函数. 如果删除async关键字,应该会开始看到预期的结果.

When using V2 of Azure functions you need to decide if you are going to be using promises or callbacks. The code you posted is callback based but exported as a promise because of the use async keyword and since the MongoClient.connect is not awaited the runtime exits your function before executing these results. If you remove the async keyword should start seeing the desired results.

这篇关于调用mongodb的azure函数(节点)成功完成但没有错误,但查询没有返回任何结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆