Javascript Can';t同步mongoose操作以返回数组

Javascript Can';t同步mongoose操作以返回数组,javascript,node.js,mongodb,express,mongoose,Javascript,Node.js,Mongodb,Express,Mongoose,作为员工应用程序管理的一部分,我希望将业务逻辑数据库操作与主应用程序文件分离。 最简单的操作是使用async/await从数据库中读取所有员工,以对其进行同步: module.exports.getEmployees = async () => { const employees = await Employee.find(); return employees; } 在我的app.js中,我键入了以下代码: const employee = require(__dirn

作为员工应用程序管理的一部分,我希望将业务逻辑数据库操作与主应用程序文件分离。 最简单的操作是使用async/await从数据库中读取所有员工,以对其进行同步:

module.exports.getEmployees = async () => {
    const employees = await Employee.find();
    return employees;
}
在我的app.js中,我键入了以下代码:

const employee = require(__dirname + "/models/employee.js");

app.get("/employees", (req, res) => {
    const employeeList =  employee.getEmployees();
    employeeList.then(res.send(employeeList));
})

但是,数组仍然显示为空?

然后promise中的
子句接受函数作为参数&此函数有一个保存实际响应的参数

像这样的-

new Promise().then((response) => {
    console.log(response);
});
你正在做
employeeList.then(res.send(employeeList))
这意味着then子句的参数是
res.send()
,它不起作用

试试这个-

employeeList.then((list) => {
    // please note the response type here depends on the Content-Type set in the response Header
    res.send(list);
    // In case of normal http server, try this -
    res.send(JSON.stringify(list));
});

我希望这能有所帮助。

那么promise中的
子句接受函数作为参数&这个函数有一个保存实际响应的参数

像这样的-

new Promise().then((response) => {
    console.log(response);
});
你正在做
employeeList.then(res.send(employeeList))
这意味着then子句的参数是
res.send()
,它不起作用

试试这个-

employeeList.then((list) => {
    // please note the response type here depends on the Content-Type set in the response Header
    res.send(list);
    // In case of normal http server, try this -
    res.send(JSON.stringify(list));
});
我希望这有帮助