【问题标题】:Arrow function in Promise does not work with semicolon (Javascript) [duplicate]Promise 中的箭头函数不适用于分号(Javascript)[重复]
【发布时间】:2021-01-28 02:44:42
【问题描述】:

我在前端使用 JS 从 python 烧瓶后端获取数据。

fetch("/get_masks", {
            method : "POST",
            headers: {
                    'Content-Type': 'application/json'
                },
            body: JSON.stringify({"image_index" : 0})
        }).then(response => response.json()).then( data => {
                console.log(data["hello"]); 
            });

烧瓶:

@app.route("/get_masks", methods=["POST"])
def get_masks():
    
    return jsonify(hello="test")

这很好用,但是如果我在响应中添加括号和分号,它就不再起作用了

.then(response => {response.json();}).then( data => {
                console.log(data["hello"]); 

然后“响应”返回一个未定义的对象。

有人知道为什么会这样吗?

提前谢谢你!

【问题讨论】:

  • 你忘记了return声明

标签: javascript flask promise fetch


【解决方案1】:

为简单起见,response => response.json() 表示 response => { return response.json(); }

所以,如果要使用大括号,请使用return

.then(response => {
  return response.json();
})
.then( data => {
  console.log(data["hello"]);
})

【讨论】:

    猜你喜欢
    • 2019-08-06
    • 1970-01-01
    • 2021-03-29
    • 2019-02-25
    • 2020-11-08
    • 2020-03-07
    • 2023-03-12
    • 1970-01-01
    • 2023-03-08
    相关资源
    最近更新 更多