【问题标题】:SQLQuery not returning document queriedSQLQuery 不返回查询的文档
【发布时间】:2019-09-25 00:53:36
【问题描述】:

带有 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",
    ........
}

函数.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 = 未定义

是否有我没有找到的各种查询监视器,以便我可以看到正在发生的事情?

提前感谢您的帮助。

【问题讨论】:

  • 这个nodejs是运行时2.0还是1.0?
  • 这是在他们的门户编辑器环境中使用 nodejs。如果是 FUNCTIONS EXTENSIONS VERSION 并且基于绑定不起作用(来自我读过的许多线程),我相信它是 2.0。那说 2. 我对这个环境太陌生了。否则,我在哪里可以找到这些信息?

标签: sql node.js azure azure-cosmosdb


【解决方案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"
    };
};

【讨论】:

  • 你太棒了。谢谢你。 myInput[0] 是关键。我对此太陌生,还没有见过这样的例子(除了在循环中进行交互)。案件结案!
  • 是的,没问题。我误读了你的问题,因为你在帖子中建议了答案,说它没有返回文档,但你没有正确引用它。
  • 旁注,我现在永远不会创建自己的身份管理解决方案,我会使用 Azure AD B2B 或 B2C,存储密码是非常糟糕的做法。
  • 因为 myInput.password 在使用 Id 和 PartitionKey 作为获取数据的方式时有效,所以我假设 myInput.password 是有效的。直到有人证明数据实际上正在被检索,问题才发生了变化。我将研究 Azure AD。无论如何,我仍然需要让 SQL 查询正常工作。再次感谢您(花了几天时间阅读和研究)。
【解决方案2】:

试试

select *
from Users
where UserName = '12345678'

【讨论】:

  • 这导致了一个错误:消息:{"errors":[{"severity":"Error","location":{"start":26,"end":34},"代码”:“SC2001”,“消息”:“无法解析标识符'用户名'。”}]} ActivityId:4cbb01f8-6af3-458e-95e7-13bca1e4f27f,Microsoft.Azure.Documents.Common/2.5.1,Windows /10.0.14393 documentdb-netcore-sdk/2.2.0
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多