TypeError: Cannot read property 'then' of undefined in Algorithmic Trading
TypeError: Cannot read property 'then' of undefined in Algorithmic Trading
出于某种原因,每次我 运行 这段代码时,我都会遇到“类型错误:无法读取未定义的 属性 'then'”错误。我不明白为什么。这是代码:
console.log("BUY");
exchange.marketBuy()
.then(res => {
console.log("Buy successful");
hasPosition = true
setTimeout(strategy, 1000)
})
.catch(console.error);
marketBuy() 函数是
marketBuy() {
client.getLatestInformation()
.then(response => {
var price = response.result[0].last_price;
client.placeActiveOrder({
side: "Buy",
symbol: "BTCUSD",
order_type: "Market",
qty: 20,
time_in_force: "GoodTillCancel",
take_profit: price * 1.5,
stop_loss: price / 1.5})
.then(response => console.log(response))
})
我试过了
console.log("BUY");
exchange.marketBuy()
.then(res => {
hasPosition = true
setTimeout(strategy, 1000)
return Promise.resolve(console.log("Buy successful"));
})
.catch(console.error);
我似乎找不到问题所在。有什么想法吗?
您有几个不同的问题。正如其他人已经指出的那样,您没有从 makeBuy()
方法返回承诺。但是,它似乎并不像建议的那样简单地添加一个 return
语句那么容易。这是因为在将 hasPosition
设置为 true 之前,您似乎实际上需要等待内部承诺从 client.placeActiveOrder()
解决。
所以你有两个选择(推荐#2):
- 将必须等待内部 promise 解析的代码移动到内部 promise 的
.then()
因此(这会产生 hasPosition
变量本身的问题):
client.placeActiveOrder({
side: "Buy",
symbol: "BTCUSD",
order_type: "Market",
qty: 20,
time_in_force: "GoodTillCancel",
take_profit: price * 1.5,
stop_loss: price / 1.5})
.then(response => {
console.log(response);
console.log("Buy successful");
hasPosition = true;
setTimeout(strategy, 1000);
});
- 使用async/await让您的代码读起来像一个工作流程:
async marketBuy() {
const response = await client.getLatestInformation();
const price = response.result[0].last_price;
const order = await client.placeActiveOrder({
side: "Buy",
symbol: "BTCUSD",
order_type: "Market",
qty: 20,
time_in_force: "GoodTillCancel",
take_profit: price * 1.5,
stop_loss: price / 1.5
})
return order;
}
推荐选择二,因为它允许您按照预期的方式编写代码。下面是不会再抛出错误的调用代码:
console.log("BUY");
exchange.marketBuy()
.then(res => { // res here is the order returned
console.log("Buy successful");
hasPosition = true
setTimeout(strategy, 1000)
})
.catch(console.error);
出于某种原因,每次我 运行 这段代码时,我都会遇到“类型错误:无法读取未定义的 属性 'then'”错误。我不明白为什么。这是代码:
console.log("BUY");
exchange.marketBuy()
.then(res => {
console.log("Buy successful");
hasPosition = true
setTimeout(strategy, 1000)
})
.catch(console.error);
marketBuy() 函数是
marketBuy() {
client.getLatestInformation()
.then(response => {
var price = response.result[0].last_price;
client.placeActiveOrder({
side: "Buy",
symbol: "BTCUSD",
order_type: "Market",
qty: 20,
time_in_force: "GoodTillCancel",
take_profit: price * 1.5,
stop_loss: price / 1.5})
.then(response => console.log(response))
})
我试过了
console.log("BUY");
exchange.marketBuy()
.then(res => {
hasPosition = true
setTimeout(strategy, 1000)
return Promise.resolve(console.log("Buy successful"));
})
.catch(console.error);
我似乎找不到问题所在。有什么想法吗?
您有几个不同的问题。正如其他人已经指出的那样,您没有从 makeBuy()
方法返回承诺。但是,它似乎并不像建议的那样简单地添加一个 return
语句那么容易。这是因为在将 hasPosition
设置为 true 之前,您似乎实际上需要等待内部承诺从 client.placeActiveOrder()
解决。
所以你有两个选择(推荐#2):
- 将必须等待内部 promise 解析的代码移动到内部 promise 的
.then()
因此(这会产生hasPosition
变量本身的问题):
client.placeActiveOrder({
side: "Buy",
symbol: "BTCUSD",
order_type: "Market",
qty: 20,
time_in_force: "GoodTillCancel",
take_profit: price * 1.5,
stop_loss: price / 1.5})
.then(response => {
console.log(response);
console.log("Buy successful");
hasPosition = true;
setTimeout(strategy, 1000);
});
- 使用async/await让您的代码读起来像一个工作流程:
async marketBuy() {
const response = await client.getLatestInformation();
const price = response.result[0].last_price;
const order = await client.placeActiveOrder({
side: "Buy",
symbol: "BTCUSD",
order_type: "Market",
qty: 20,
time_in_force: "GoodTillCancel",
take_profit: price * 1.5,
stop_loss: price / 1.5
})
return order;
}
推荐选择二,因为它允许您按照预期的方式编写代码。下面是不会再抛出错误的调用代码:
console.log("BUY");
exchange.marketBuy()
.then(res => { // res here is the order returned
console.log("Buy successful");
hasPosition = true
setTimeout(strategy, 1000)
})
.catch(console.error);