[英]Function returns undefined nodejs
Here I'm creating an array that have property 1, then I'm trying to filter the results with only the highest number and then output it when calling the function, but it returns undefined.在这里,我正在创建一个具有属性 1 的数组,然后我尝试仅过滤具有最高数字的结果,然后在调用函数时将其输出,但它返回未定义。 I'm trying to find to figure out what I'm doing wrong here.
我试图找出我在这里做错了什么。 Thanks
谢谢
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
You haven't returned anything from that function.你没有从那个函数返回任何东西。
You should add return
before table.find
您应该在
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());
If toArray
does not return values in that callback function.如果
toArray
没有在该回调函数中返回值。 You can assign a value and return it.您可以分配一个值并返回它。
Note that this approach will work if toArray
and find
do not return promises请注意,如果
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());
The 3rd approach, if find
and toArray
are actually promises第三种方法,如果
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());
If toArray
does not return results, you can assign variable again, but the difference is now we have async/await
如果
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());
Your main issue is that calcDifference()
doesn't have a return value.您的主要问题是
calcDifference()
没有返回值。 To solve this you first need to change the way you call toArray()
.要解决这个问题,您首先需要更改调用
toArray()
的方式。 Like Barman pointed out in the comments:就像巴曼在评论中指出的那样:
When
toArray()
is called with a callback function, it doesn't return anything.当使用回调函数调用
toArray()
时,它不会返回任何内容。 See mongodb.github.io/node-mongodb-native/4.5/classes/… – Barmar见mongodb.github.io/node-mongodb-native/4.5/classes/… – Barmar
When you look at the documentation you'll find that it does return a promise if you do not pass a callback function.当您查看文档时,您会发现如果您不传递回调函数,它确实会返回一个承诺。 Firstly we change
calcDifference
to an async function
.首先,我们将
calcDifference
更改为async function
。 Then we await table.find({ "entries": 1 }).toArray()
instead of passing a callback.然后我们
await table.find({ "entries": 1 }).toArray()
而不是传递回调。 After that you can do your other stuff.之后你就可以做你的其他事情了。
Do note that an async function always returns a promise, so to use the return value you either have to await
the value, or use a then()
construct if you are not in async context.请注意,异步函数总是返回一个 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.