我尝试聚合我的两个MongoDB数据库,但是现在我从收集信息中遇到了问题。
所以,我尝试做的是从两个数据库中创建一个object
。
我的问题是函数User.find()
和EmployersSchemaDB.find()
在聚合后不会将变量/ obj推到外面。你可以在下面的代码中看到它......
我做错了什么?
UPD根据评论中的回答:
router.route('/employers')
.get((req, res) => {
function getDate() {
console.log('Hi');
let obj = {
userCookies: '',
EmployersObj: {}
}
const userFind = User.find((err, users) => {
if (err) { res.send(err) }
if (req.session.userId !== undefined) {
obj.userCookies = req.session.userId;
return obj.userCookies;
} else {
obj.userCookies = '';
return obj.userCookies;
}
});
const employerFind = EmployersSchemaDB.find((err, employers) => {
if (err) { res.send(err) }
obj.EmployersObj = employers;
return obj.EmployersObj;
});
Promise.all(userFind, employerFind).then(responseArr => {
return res.json(responseArr);
}).catch(err => console.log(err));
}
getDate();
})
错误日志:
Server running on port 3016
19:23:24 Server.1 | Hi
19:23:24 Server.1 | TypeError: undefined is not a function
19:23:24 Server.1 | at Function.all (<anonymous>)
19:23:24 Server.1 | at getDate (C:\Users\\Desktop\react\mern\Server\server.js:194:21)
19:23:24 Server.1 | at router.route.get (C:\Users\\Desktop\react\mern\Server\server.js:198:9)
19:23:24 Server.1 | at Layer.handle [as handle_request] (C:\Users\\Desktop\react\mern\node_modules\express\lib\router\layer.js:95:5)
19:23:24 Server.1 | at next (C:\Users\\Desktop\react\mern\node_modules\express\lib\router\route.js:137:13)
19:23:24 Server.1 | at Route.dispatch (C:\Users\\Desktop\react\mern\node_modules\express\lib\router\route.js:112:3)
19:23:24 Server.1 | at Layer.handle [as handle_request] (C:\Users\\Desktop\react\mern\node_modules\express\lib\router\layer.js:95:5)
19:23:24 Server.1 | at C:\Users\\Desktop\react\mern\node_modules\express\lib\router\index.js:281:22
19:23:24 Server.1 | at Function.process_params (C:\Users\\Desktop\react\mern\node_modules\express\lib\router\index.js:335:12)
19:23:24 Server.1 | at next (C:\Users\\Desktop\react\mern\node_modules\express\lib\router\index.js:275:10)
19:23:24 Server.1 | at Function.handle (C:\Users\\Desktop\react\mern\node_modules\express\lib\router\index.js:174:3)
19:23:24 Server.1 | at router (C:\Users\\Desktop\react\mern\node_modules\express\lib\router\index.js:47:12)
19:23:24 Server.1 | at Layer.handle [as handle_request] (C:\Users\\Desktop\react\mern\node_modules\express\lib\router\layer.js:95:5)
19:23:24 Server.1 | at trim_prefix (C:\Users\\Desktop\react\mern\node_modules\express\lib\router\index.js:317:13)
19:23:24 Server.1 | at C:\Users\\Desktop\react\mern\node_modules\express\lib\router\index.js:284:7
19:23:24 Server.1 | at Function.process_params (C:\Users\\Desktop\react\mern\node_modules\express\lib\router\index.js:335:12)
ayer.js:95:5)
答案 0 :(得分:0)
可能因为您的EmployersSchemaDB.find()
方法返回了一个承诺。您可以使用Promise.all执行两个查询,然后在完成后处理响应:
router
.route('/employers')
.get((req, res) => {
const userFind = User.find();
const employerFind = EmployersSchemaDB.find();
Promise.all([userFind, employerFind])
.then(responseArr => {
return res.json(formatResponse(responseArr, '' || req.session.userId));
})
.catch(err => console.log(err));
})
function formatResponse(arr, cookies) {
console.log('formatRes hi', arr);
const obj = {
userCookies: cookies,
usersRes = arr[0],
employersRes: arr[1]
}
}