gpt4 book ai didi

javascript - TypeError : Cannot read property 'then' of undefined. 当我尝试运行 updatefirst 函数时出现此错误

转载 作者:行者123 更新时间:2023-12-02 21:29:21 24 4
gpt4 key购买 nike

我正在尝试运行 updatefirst 函数,但即使我将预定义值传递给 get_plo_amount 中的解析函数,也会一次又一次出现相同的错误。console.log(result) 行运行表明没有问题获取数据。我不知道我在这里做错了什么:(。任何帮助将不胜感激。谢谢。

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
})
}

最佳答案

您应该返回一个 promise ,以使 then() 方法起作用。尝试像这样更新你的函数:

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)
})
})

}

关于javascript - TypeError : Cannot read property 'then' of undefined. 当我尝试运行 updatefirst 函数时出现此错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60660205/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com