简体   繁体   English

TypeError:无法读取未定义(Promise)(node.js)的属性“ then”

[英]TypeError: Cannot read property 'then' of undefined (Promise) (node.js)

When my API calls addSub() which returns a promise, The inner promise is able to return data to outer promise, but the outer is unable to return data to AP while giving the following output. 当我的API调用返回承诺的addSub() ,内部的addSub()可以将数据返回给外部的addSub() ,但是外部的addSub()不能将数据返回给AP,同时提供以下输出。

a+b: 9 a + b:9

addSub data: 9 addSub数据:9

pro: Promise { 8 } 赞成:承诺{8}

TypeError: Cannot read property 'then' of undefined TypeError:无法读取未定义的属性“ then”

function add(a, b){
  console.log("a+b: ",a+b)
  return new Promise((resolve, reject) => {
    resolve(a+b)
    })
}

function addSub(){
  add(4,5)
    .then((data) => {
        console.log("addSub data: ", data)
        var pro = new Promise((resolve, reject) => {
          resolve(data - 1)
        })
        console.log("pro: ",pro)
        return pro
    })
}

app.get('/promise', function (req, res){
    addSub()
    .then((data) => {
        console.log("final res: ", data)
        res.send({"data": data})
    })
});

You must add a return before add(4,5) : 您必须在add(4,5)之前添加一个返回值:

...    
function addSub(){
            return add(4,5)
            .then((data) => {
            ...

 function add(a, b){ console.log("a+b: ",a+b) return new Promise((resolve, reject) => { resolve(a+b) }) } function addSub(){ return add(4,5) .then((data) => { console.log("addSub data: ", data) var pro = new Promise((resolve, reject) => { resolve(data - 1) }) console.log("pro: ",pro) return pro }) } addSub() .then((data) => { console.log("final res: ", data) res.send({"data": data}) }) 

Just put a return statement before call add function inside your addSub function 只需在addSub函数内的调用add函数之前放一个return语句

function addSub() {
    return add(4, 5)
        .then((data) => {
            console.log("addSub data: ", data)
            var pro = new Promise((resolve, reject) => {
                resolve(data - 1)
            })
            console.log("pro: ", pro)
            return pro
        })
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 节点Promise-TypeError无法读取未定义的属性.then - Node Promise - TypeError cannot read property .then of undefined Node.js TypeError:无法读取未定义的属性“主机” - Node.js TypeError: Cannot read property 'host' of undefined 类型错误:无法读取未定义的 Node.js 的属性 - TypeError: Cannot read property of undefined Node.js Node.js-TypeError:无法读取未定义的属性“ document” - Node.js - TypeError: Cannot read property 'document' of undefined Node.js中的TypeError:无法读取未定义的属性“ 0” - TypeError in node.js: cannot read property '0' of undefined 未捕获的类型错误:无法读取 node.js 中未定义的属性“通道” - Uncaught TypeError: Cannot read property 'channel' of undefined in node.js Node.js 抛出 TypeError:无法读取未定义的属性“testModule” - Node.js throwing TypeError: Cannot read property 'testModule' of undefined TypeError:无法读取未定义的属性“ get”(Node.js) - TypeError: Cannot read property 'get' of undefined (Node.js) TypeError:无法读取node.js中未定义的属性“名称” - TypeError: Cannot read property 'name' of undefined in node.js Node.js UnhandledPromiseRejection:TypeError:无法读取未定义的属性“ sign” - Node.js UnhandledPromiseRejection: TypeError: Cannot read property 'sign' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM