I'm trying to get the hang of using Mongoose promises with the async/await functionality of Node.js. When my function printEmployees
is called I want to save the list of employees which are queried by the orderEmployees
function. While, the console.log
statement inside orderEmployees
returns the expected query, the console.log
inside of printEmployees
returns undefined
, suggesting that I'm not returning the promise correctly.
我正试图通过Node.js的async / await功能来使用Mongoose promises。当我的函数printEmployees被调用时,我想保存orderEmployees函数查询的员工列表。虽然orderEmployees中的console.log语句返回了预期的查询,但printEmployees中的console.log返回undefined,表明我没有正确地返回promise。
I'm new to promises so entirely possible that I'm not correctly understanding the paradigm... any help is much appreciated.
我很有可能完全有可能,我没有正确地理解这个范例......任何帮助都非常感激。
printEmployees: async(company) => {
var employees = await self.orderEmployees(company);
// SECOND CONSOLE.LOG
console.log(employees);
},
orderEmployees: (companyID) => {
User.find({company:companyID})
.exec()
.then((employees) => {
// FIRST CONSOLE.LOG
console.log(employees);
return employees;
})
.catch((err) => {
return 'error occured';
});
},
4 个解决方案
#1
11
You need to return
your Promise
, otherwise you are awaiting on a function that returns undefined
.
您需要返回Promise,否则您正在等待返回undefined的函数。
orderEmployees: (companyID) => {
return User.find({ company:companyID }).exec()
}
You can only await
Promises; or a function marked as async
, which essentially returns a Promise
.
你只能等待Promises;或者标记为异步的函数,它实质上返回一个Promise。
#2
11
In order to make orderEmployees
behave like async functions, you have to return the resulting promise. There are two rules to follow when using promises without async/await
keywords:
为了使orderEmployees表现得像异步函数,您必须返回结果承诺。使用没有async / await关键字的promises时要遵循两条规则:
- A function is asynchronous if it returns a
Promise
- 如果函数返回Promise,则该函数是异步的
- If you have a promise (for example returned by an async function) you must either call
.then
on it or return it. - 如果您有一个承诺(例如由异步函数返回),您必须在其上调用.then或返回它。
When you are using async/await
then you must await
on promises you obtain.
当您使用async / await时,您必须等待您获得的承诺。
This said you will notice that you do not return the promise generated inside orderEmployees
. Easy to fix, but its also easy to rewrite that function to async too.
这表示你会注意到你没有返回orderEmployees中生成的promise。易于修复,但也很容易将该功能重写为异步。
orderEmployees: (companyID) => {
return User.find({company:companyID}) // Notice the return here
.exec()
.then((employees) => {
// FIRST CONSOLE.LOG
console.log(employees);
return employees;
})
.catch((err) => {
return 'error occured';
});
},
or
要么
orderEmployees: async(companyID) => {
try {
const employees = await User.find({company:companyID}).exec();
console.log(employees);
return employees;
} catch (err) {
return 'error occured';
}
},
PS: the error handling is somewhat flawed here. We usually do not handle errors by returning an error string from a function. It is better to have the error propagate in this case, and handle it from some top-level, UI code.
PS:错误处理在这里有些缺陷。我们通常不会通过从函数返回错误字符串来处理错误。在这种情况下,最好让错误传播,并从一些*UI代码处理它。
#3
4
You are not returning a Promise from orderEmployees.
您没有从orderEmployees返回Promise。
printEmployees: async(company) => {
var employees = await self.orderEmployees(company);
// SECOND CONSOLE.LOG
console.log(employees);
},
orderEmployees: (companyID) => {
return User.find({company:companyID})
.exec()
.then((employees) => {
// FIRST CONSOLE.LOG
console.log(employees);
return employees;
})
.catch((err) => {
return 'error occured';
});
},
#4
2
You need to return a Promise
from orderEmployees
您需要从orderEmployees返回Promise
orderEmployees: companyId => User.find({ companyId }).exec()
If you want to do some error handling or pre-processing before you return then you can keep your code as is but just remember to return the result (promises are chainable).
如果你想在返回之前做一些错误处理或预处理,那么你可以保持你的代码不变,但只记得返回结果(promises是可链接的)。
#1
11
You need to return
your Promise
, otherwise you are awaiting on a function that returns undefined
.
您需要返回Promise,否则您正在等待返回undefined的函数。
orderEmployees: (companyID) => {
return User.find({ company:companyID }).exec()
}
You can only await
Promises; or a function marked as async
, which essentially returns a Promise
.
你只能等待Promises;或者标记为异步的函数,它实质上返回一个Promise。
#2
11
In order to make orderEmployees
behave like async functions, you have to return the resulting promise. There are two rules to follow when using promises without async/await
keywords:
为了使orderEmployees表现得像异步函数,您必须返回结果承诺。使用没有async / await关键字的promises时要遵循两条规则:
- A function is asynchronous if it returns a
Promise
- 如果函数返回Promise,则该函数是异步的
- If you have a promise (for example returned by an async function) you must either call
.then
on it or return it. - 如果您有一个承诺(例如由异步函数返回),您必须在其上调用.then或返回它。
When you are using async/await
then you must await
on promises you obtain.
当您使用async / await时,您必须等待您获得的承诺。
This said you will notice that you do not return the promise generated inside orderEmployees
. Easy to fix, but its also easy to rewrite that function to async too.
这表示你会注意到你没有返回orderEmployees中生成的promise。易于修复,但也很容易将该功能重写为异步。
orderEmployees: (companyID) => {
return User.find({company:companyID}) // Notice the return here
.exec()
.then((employees) => {
// FIRST CONSOLE.LOG
console.log(employees);
return employees;
})
.catch((err) => {
return 'error occured';
});
},
or
要么
orderEmployees: async(companyID) => {
try {
const employees = await User.find({company:companyID}).exec();
console.log(employees);
return employees;
} catch (err) {
return 'error occured';
}
},
PS: the error handling is somewhat flawed here. We usually do not handle errors by returning an error string from a function. It is better to have the error propagate in this case, and handle it from some top-level, UI code.
PS:错误处理在这里有些缺陷。我们通常不会通过从函数返回错误字符串来处理错误。在这种情况下,最好让错误传播,并从一些*UI代码处理它。
#3
4
You are not returning a Promise from orderEmployees.
您没有从orderEmployees返回Promise。
printEmployees: async(company) => {
var employees = await self.orderEmployees(company);
// SECOND CONSOLE.LOG
console.log(employees);
},
orderEmployees: (companyID) => {
return User.find({company:companyID})
.exec()
.then((employees) => {
// FIRST CONSOLE.LOG
console.log(employees);
return employees;
})
.catch((err) => {
return 'error occured';
});
},
#4
2
You need to return a Promise
from orderEmployees
您需要从orderEmployees返回Promise
orderEmployees: companyId => User.find({ companyId }).exec()
If you want to do some error handling or pre-processing before you return then you can keep your code as is but just remember to return the result (promises are chainable).
如果你想在返回之前做一些错误处理或预处理,那么你可以保持你的代码不变,但只记得返回结果(promises是可链接的)。