简体   繁体   English

类型错误:无法读取未定义的属性“then”。 当我尝试运行 updatefirst 函数时出现此错误

[英]TypeError: Cannot read property 'then' of undefined. Getting this error when I am trying to run updatefirst function

I am trying to run the updatefirst function but getting the same error again and again even when I pass a predefined value to the resolve function in get_plo_amount.The console.log(result) line runs which shows than there was no issue in fetching the data.我正在尝试运行 updatefirst 函数,但即使我将预定义的值传递给 get_plo_amount 中的解析函数时,也会一次又一次地得到相同的错误。 console.log(result) 行运行表明,在获取数据时没有问题. I don't know what I am doing wrong here :( .Any help will be appreciated. Thanks .我不知道我在这里做错了什么:(。任何帮助将不胜感激。谢谢。

const get_plo_amount = function(p){
    plo.findOne({phone : p}).then((result) => {
        console.log(result) //this line is running
        return new Promise((resolve,reject) => {
            resolve(result.daily_amount)
        })

    }).catch((e) => {
        console.log("catch")
        return new Promise((resolve,reject) => {
            reject(e)
        })
    })

}

//updatefirst
const updatefirst = function(plo,date){
    driver.find({associated_plo : plo}).then((result) => {
        //console.log(result)//delete this
        get_plo_amount(plo).then((amount) => {
            console.log(amount)
            var arr
            for(i=0;i<result.length;i++){
                var pdue = parseInt(result[i].balance) + amount
                var d_obj = {
                    driver : result[i].name,
                    phone : result[i].phone,
                    auto_number : result[i].auto_number,
                    amount : pdue,
                }
             //   console.log(d_obj)//delete this
                arr[i] = d_obj
            }

            const obj = {
                associated_plo : plo,
                date : date,
                earning : "0",
                payments : arr
            }
            const t = new transactions(obj)

            t.save().then(() => {
                return "success"
            }).catch((e) => {
                return e
            })

        }).catch((e) => {
            console.log(e)
            return e
        })


    }).catch((e) => {
        console.log(e)
        return e
    })
}

You should return a promise, to make the then() method work.您应该返回一个承诺,以使then()方法工作。 Try updating your function like this:尝试像这样更新你的函数:

const get_plo_amount = function(p){
    return plo.findOne({phone : p}).then((result) => {
        console.log(result) //this line is running
        return new Promise((resolve,reject) => {
            resolve(result.daily_amount)
        })

    }).catch((e) => {
        console.log("catch")
        return new Promise((resolve,reject) => {
            reject(e)
        })
    })

}

2 more changes还有 2 个变化

  1. you can return result.daily_amount without creating another promise您可以返回result.daily_amount而无需创建另一个承诺
  2. the promise in the catch is rejecting so you might as well remove the catch catch 中的 promise 被拒绝,所以你不妨删除 catch
const get_plo_amount = function(p){
    return plo.findOne({phone : p}).then((result) => {
        console.log(result) //this line is running
        result.daily_amount;
    });
}

暂无
暂无

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

相关问题 TypeError:无法读取未定义的属性&#39;addTopic&#39;。 我是盲人吗? - TypeError: Cannot read property 'addTopic' of undefined. Am I blind? 运行我的nodejs代码时收到此错误:&#39;TypeError:无法读取未定义的属性&#39;apply&#39;。 &#39; - I get this error when running my nodejs code: ' TypeError: Cannot read property 'apply' of undefined. ' 我正在尝试为三个单独的选项卡运行 onEdit 函数,但收到 TypeError:无法读取未定义的属性“值” - I am trying to run the onEdit function for three separate tabs but receiving TypeError: Cannot read property 'value' of undefined 我正在尝试调度一个操作,但收到此错误:“未处理的拒绝(TypeError):无法读取未定义的属性'类型'” - I am trying to dispatch a action but getting this error: “ Unhandled Rejection (TypeError): Cannot read property 'type' of undefined” 运行此程序后出现错误,Uncaught TypeError:无法读取未定义的属性“ push” - I am getting error after run this program, Uncaught TypeError: Cannot read property 'push' of undefined 我正在尝试从数据库中获取 map mydata 但我收到此错误。 TypeError:无法读取未定义的属性“地图” - I am trying to map mydata from the database but i am getting this error. TypeError: Cannot read property 'map' of undefined 错误类型错误:无法读取未定义的属性“toLowerCase”。 离子 3 - ERROR TypeError: Cannot read property 'toLowerCase' of undefined. Ionic 3 × TypeError:无法读取未定义的属性“地图”。 发现错误 - × TypeError: Cannot read property 'map' of undefined. found error “ typeError:无法读取未定义的属性“用户名”。” - “typeError: Cannot read property 'username' of undefined.” TypeError:无法读取未定义的属性“名称”。 我找不到出现此错误的原因。 我使用 mysql、nodejs 和 ejs 作为视图引擎 - TypeError: Cannot read property 'name' of undefined. I cannot find the reason why I get this error. I am using mysql, nodejs and ejs as view engine
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM