(!) Issue cannot be reproduced locally.
Azure Function Version ~4
Node Version 14.18.1

创建一个简单的HTTP触发Azure函数并只设置两个简单属性,我们得到以下代码:

module.exports = async function (context, req) {

    req.auth = {authField: 'some value'}
    req.user = {userField: 'some value'}
    context.log(`${JSON.stringify(req,null,2)}`);

    context.res = {
        body: 'responseMessage'
    };
}

记录器打印以下对象:

{
  "method": "POST",
  "url": "xxx",
  "originalUrl": "xxx",
  "headers": {
    /// ...
  },
  "query": {},
  "params": {},
  "body": { "name": "Azure" },
  "rawBody": "{\"name\":\"Azure\"}",
  "auth": { "authField": "some value" }
}

正如你所看到的,只有auth被设置,而不是user.

当我使用Azure Function~3进行测试时,输出如下所示:

{
  "method": "POST",
  "url": "xxx",
  "originalUrl": "xxx",
  "headers": {
    // ...
  },
  "query": {},
  "params": {},
  "body": { "name": "Azure" },
  "rawBody": "{\"name\":\"Azure\"}",
  "auth": { "authField": "some value" },
  "user": { "userField": "some value" }
}

正如你所见,这个字段已经设置好了.

字段user由我们通过express-jwt(v6.1.0)使用,当没有为requestProperty提供值时,它是using.

我还不能复制它,但在从Typescript项目传输出来的过程中,我们得到了以下运行时错误:

FailureException: Cannot set property user of [object Object] which has only a getterStack: TypeError: Cannot set property user of [object Object] which has only a getterat Object.run

这一问题始于2022年4月26日.

问题:

  • 原因是什么?
  • 是否可以快速回滚到运行正常的Azure功能自定义版本?

推荐答案

我在this PR中找到了罪魁祸首,它是为Azure Function runtime v4添加的.2.0.

The user field was added with only a getter.
We took the code and made the minimal example:

class Request {
    #cachedUser?: string | null;
    constructor() {
    }
    get user(): string | null {
        if (this.#cachedUser === undefined) {
            this.#cachedUser = "some value";
        }
        return this.#cachedUser;
    }
}

并得到了以下透明版本:

var __classPrivateFieldGet =
    (this && this.__classPrivateFieldGet) ||
    function (receiver, state, kind, f) {
        if (kind === 'a' && !f) throw new TypeError('Private accessor was defined without a getter')
        if (typeof state === 'function' ? receiver !== state || !f : !state.has(receiver))
            throw new TypeError('Cannot read private member from an object whose class did not declare it')
        return kind === 'm' ? f : kind === 'a' ? f.call(receiver) : f ? f.value : state.get(receiver)
    }
var __classPrivateFieldSet =
    (this && this.__classPrivateFieldSet) ||
    function (receiver, state, value, kind, f) {
        if (kind === 'm') throw new TypeError('Private method is not writable')
        if (kind === 'a' && !f) throw new TypeError('Private accessor was defined without a setter')
        if (typeof state === 'function' ? receiver !== state || !f : !state.has(receiver))
            throw new TypeError('Cannot write private member to an object whose class did not declare it')
        return kind === 'a' ? f.call(receiver, value) : f ? (f.value = value) : state.set(receiver, value), value
    }
var _Request_cachedUser
class Request {
    constructor() {
        _Request_cachedUser.set(this, void 0)
    }
    get user() {
        if (__classPrivateFieldGet(this, _Request_cachedUser, 'f') === undefined) {
            __classPrivateFieldSet(this, _Request_cachedUser, 'some value', 'f')
        }
        return __classPrivateFieldGet(this, _Request_cachedUser, 'f')
    }
}
_Request_cachedUser = new WeakMap()

// this section is added for testing
const request = new Request()
request.user = 'new value'
console.log(JSON.stringify(request.user))

因此,它总是返回初始值,在我们的示例中是"some value",但在原始代码中只是undefined,不允许设置它.

Node.js相关问答推荐

查询嵌套数组中的最后一个元素具有特定值的mongoDB集合中的文档

Gmail API获取附件PDF未正确呈现.我遗漏了什么?

请求正文未定义

如何修复PostgreSQL和NodeJS/NestJS应用程序之间的日期时间和时区问题?

后端如何保护数据不被用户提取其他用户的数据?

Express Web 服务器部署到 prod 但 GET 返回超时错误

Redis Typescript 删除方法类型转换

TypeScript Eslint警告了一个AWS客户端构造函数(dynamodb),但没有警告另一个(s3)

运行 cypress 测试时如何指定 .env 文件用于我的开发服务器?

Mocha调用所有it回调模拟(测试中间件)

React Native:执行 com.android.build.gradle.internal.tasks.Workers$ActionFacade 时发生故障

MongoDB Atlas中的聚合触发器不起作用

在express js模型中将js转换为Typescript 时Typescript 错误

MERN 堆栈项目中的 React [create-react-app] 正在提供依赖项

如果 express.js (node.js) http 请求在完成之前关闭会发生什么?

如何将使用 Gulp 的 node 部署到 heroku

如何使用 Puppeteer 从输入中删除现有文本?

错误:大多数中间件(如 bodyParser)不再与 Express Bundle

使用 Mongoose 进行多对多映射

react-native run-android 无法识别