SQLQuery不返回查询的文档

时间:2019-09-24 14:08:08

标签: sql node.js azure azure-cosmosdb

带有Cosmos DB + nodejs的Azure Function App无法与SQLQuery(输入)一起使用。没有文件退回。

我已经成功获得一个Id / PartionKey组合以返回文档。我在输入的SQLQuery字段中尝试了许多SQL查询组合,例如从文档无济于事。没有错误,也没有文档返回。我希望我的查询是:

SELECT * from Users where Users.UserName = '12345'

我尝试过在单引号中使用和不使用值。我已经尝试过硬编码(无绑定)查询,例如:

SELECT * from Users where Users.UserName = 12345

以及具有约束力的(在{userName}周围使用单引号和不使用单引号):

SELECT * from Users where Users.UserName = {userName}

我尝试通过将{userName}放入路由中来>

帐户/登录名/ {用户名}

并且我也尝试过使用查询中的userName(带或不带单引号):

SELECT * from Users where Users.UserName = '{Query.userName}'

分区密钥为:/ id

{
    "indexingMode": "consistent",
    "automatic": true,
    "includedPaths": [
        {
            "path": "/*"
        }
    ],
    "excludedPaths": [
        {
            "path": "/\"_etag\"/?"
        }
    ]
}

我已经进行了许多小时的搜索和阅读,并且我知道SQLQuery可能无法完全支持绑定,但是,即使对值进行硬编码也无法返回文档。

示例文档:

{
    "id": "e90ece01373e5d011fdaef2c20d9717b",
    "UserName": "17002",
    "password": "newpassword",
    "Address": "123 West ",
    "state": "FL",
    "city":  "mycity",
    "zip": "32222",
    "phoneNumber": "3215551212",
    "emailId": "email@email.com",
    "refNo": "0",
    "aboutUs": "someplace",
    ........
}

Function.json:

{
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "post",
        "get"
      ],
      "route": "Account/login/{userName}"
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    },
    {
      "type": "cosmosDB",
      "name": "myInput",
      "databaseName": "cdel",
      "collectionName": "Users",
      "connectionStringSetting": "cdel_DOCUMENTDB",
      "direction": "in",
      "sqlQuery": "SELECT * FROM Users where Users.UserName = {userName}"
    }
  ]
}

index.js:

module.exports = async function (context, req, myInput) {
    context.log('JavaScript queue trigger function processed work item');
    context.log('Passed in: ' + context.bindingData.userName);
    if (!myInput)
    {
        context.log("UserName not found");
        context.res = {
            status: 400,
            body: "UserName not found."
        }
    }
    else
    {
        context.log(req.query.password);
        context.log(myInput.password);
        context.log(context.bindings.myInput.password);

        if (myInput.password != req.query.password) {
            context.res = {
                status: 400,
                body: "Invalid logon."
            }
        }
        else {
              .....
        }
    }
    context.done();
};

期望返回单个文档。不返回任何内容。 myInput.password =未定义

是否有我找不到的查询监视器,以便可以看到发生了什么事?

预先感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

Cosmos正在返回集合,您需要从中获取第一个元素。

context.bindings.myInput[0].password

所以代码是

module.exports = async function (context, req) {
    context.log('password: ' + req.body.password);
    if (context.bindings.myInput.length > 0)
        context.log('password cosmos: ' + context.bindings.myInput[0].password);
    context.res = {
        body: "OK"
    };
};

答案 1 :(得分:0)

尝试

select *
from Users
where UserName = '12345678'