[英]Function returns undefined nodejs
在這里,我正在創建一個具有屬性 1 的數組,然后我嘗試僅過濾具有最高數字的結果,然后在調用函數時將其輸出,但它返回未定義。 我試圖找出我在這里做錯了什么。 謝謝
function calcDifference() {
table.find({
"entries": 1
}) // select entries with "entries":1
.toArray(function(err, res_spr) { // create an array of found elements
let pop = res_spr.pop(); // delete last element of the array
let storeArraySpr = []; // new array to store property "number"
for (let i = 0; i < res_spr.length; i++) {
storeArraySpr.push(res_spr[i].number);
}
var resultSpr = Math.max(...storeArraySpr); // select highest entry
return resultSpr.toFixed(8); // meant to output the result when calling the function
});
}
console.log(calcDifference()); // returns undefined
你沒有從那個函數返回任何東西。
您應該在table.find
之前添加return
function calcDifference(){
return table.find({"entries":1}) // select entries with "entries":1
.toArray(function(err, res_spr) { // create an array of found elements
let pop = res_spr.pop(); // delete last element of the array
let storeArraySpr = []; // new array to store property "number"
for (let i = 0; i < res_spr.length; i++) {
storeArraySpr.push(res_spr[i].number);
}
var resultSpr = Math.max(...storeArraySpr); // select highest entry
return resultSpr.toFixed(8); // meant to output the result when calling the function
});
}
console.log(calcDifference());
如果toArray
沒有在該回調函數中返回值。 您可以分配一個值並返回它。
請注意,如果toArray
和find
不返回承諾,則此方法將起作用
function calcDifference(){
let result;
table.find({"entries":1}) // select entries with "entries":1
.toArray(function(err, res_spr) { // create an array of found elements
let pop = res_spr.pop(); // delete last element of the array
let storeArraySpr = []; // new array to store property "number"
for (let i = 0; i < res_spr.length; i++) {
storeArraySpr.push(res_spr[i].number);
}
var resultSpr = Math.max(...storeArraySpr); // select highest entry
result = resultSpr.toFixed(8); // meant to output the result when calling the function
});
return result; //return the final result from the callback function
}
console.log(calcDifference());
第三種方法,如果find
和toArray
實際上是承諾
async function calcDifference(){
return await table.find({"entries":1}) // select entries with "entries":1
.toArray(function(err, res_spr) { // create an array of found elements
let pop = res_spr.pop(); // delete last element of the array
let storeArraySpr = []; // new array to store property "number"
for (let i = 0; i < res_spr.length; i++) {
storeArraySpr.push(res_spr[i].number);
}
var resultSpr = Math.max(...storeArraySpr); // select highest entry
return resultSpr
});
}
console.log(await calcDifference());
如果toArray
沒有返回結果,你可以再次賦值變量,但不同的是現在我們有async/await
async function calcDifference(){
let result;
await table.find({"entries":1}) // select entries with "entries":1
.toArray(function(err, res_spr) { // create an array of found elements
let pop = res_spr.pop(); // delete last element of the array
let storeArraySpr = []; // new array to store property "number"
for (let i = 0; i < res_spr.length; i++) {
storeArraySpr.push(res_spr[i].number);
}
var resultSpr = Math.max(...storeArraySpr); // select highest entry
result = resultSpr
});
return result
}
console.log(await calcDifference());
您的主要問題是calcDifference()
沒有返回值。 要解決這個問題,您首先需要更改調用toArray()
的方式。 就像巴曼在評論中指出的那樣:
當使用回調函數調用
toArray()
時,它不會返回任何內容。 見mongodb.github.io/node-mongodb-native/4.5/classes/… – Barmar
當您查看文檔時,您會發現如果您不傳遞回調函數,它確實會返回一個承諾。 首先,我們將calcDifference
更改為async function
。 然后我們await table.find({ "entries": 1 }).toArray()
而不是傳遞回調。 之后你就可以做你的其他事情了。
請注意,異步函數總是返回一個 Promise,因此要使用返回值,您必須await
該值,或者如果您不在異步上下文中,則使用then()
構造。
async function calcDifference() {
const res_spr = await table.find({ "entries": 1 }).toArray(); // select entries with "entries":1
let pop = res_spr.pop(); // delete last element of the array
let storeArraySpr = []; // new array to store property "number"
for (let i = 0; i < res_spr.length; i++) {
storeArraySpr.push(res_spr[i].number);
}
var resultSpr = Math.max(...storeArraySpr); // select highest entry
return resultSpr.toFixed(8); // meant to output the result when calling the function
}
// when in async context
console.log(await calcDifference());
// when not in async context
calcDifference().then(console.log);
// or if you need to execute multiple statements
calcDifference().then((result) => {
console.log(result);
// other stuff
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.