gpt4 book ai didi

javascript - 返回 undefined variable

转载 作者:行者123 更新时间:2023-12-03 00:55:49 25 4
gpt4 key购买 nike

我正在编写一个 Javascript 函数,它将返回一个数字作为结果。这是我的代码:

get_tiquete_hacienda: function (){
var myconsecutivo = 0;

var rpc2 = require('web.rpc');
rpc2.query({
model: 'pos.order',
method: 'compute_sales_bsi'
}).then(function(res) {
myconsecutivo = res;
console.log('soy el otro: ' + myconsecutivo);
});
return myconsecutivo;
}

如果我查看控制台,“soy el otro:”会呈现正确的值但返回 myconsecutivo 未定义

为什么会这样?

最佳答案

get_tiquete_hacienda: function (){
var myconsecutivo = 0;

var rpc2 = require('web.rpc');
return rpc2.query({
model: 'pos.order',
method: 'compute_sales_bsi'
}).then(function(res) {
myconsecutivo = res;
console.log('soy el otro: ' + myconsecutivo);
return myconsecutivo;
});
}

get_tiquete_hacienda().then(function (myconsecutivo) {
// myconsecutivo is correct here
});

或更简洁地说:

get_tiquete_hacienda: function (){  
var rpc2 = require('web.rpc');
return rpc2.query({
model: 'pos.order',
method: 'compute_sales_bsi'
}).then(function(res) {
console.log('soy el otro: ' + res);
return res;
});
}

get_tiquete_hacienda().then(function (myconsecutivo) {
// myconsecutivo is correct here
});

关于javascript - 返回 undefined variable ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52844296/

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