몽구스와 관련된 다음 기능이 있습니다.
let username = userObject.username;
Purchase.find({
account: username,
fufilled: true
})
.populate("keys")
.exec(function(err, foundPurchases) {
if (err) {
return inStockItems;
} else {
if (foundPurchases.length === 0) {
return inStockItems;
} else {
// these lists will be a list of IDs of game detail entries in the database
let listOfReceivedIds = foundPurchases.keys.map(obj => obj.game);
for (let i = 0; i < inStockItems.length; i++) {
if (inStockItems.length <= minimum) {
return inStockItems;
}
let currentProductAnalysing = inStockItems[i];
if (listOfReceivedIds.includes(currentProductAnalysing._id)) {
console.log("removing product");
inStockItems.splice(i, 1);
}
}
return inStockItems;
}
}
});
다음과 같은 함수를 실행 중입니다. undefined
inStockItems = function_name(inStockItems, userObject, amount);
함수가 inStockItems
아닌 값을 반환하도록 함수를 어떻게 다시 작성할 수 있습니까 undefined
? 감사.
.exec
Mongoose에서는 콜백을 제공하지 않으면 쿼리 결과에 대한 약속을 반환합니다. 여기 .exec()
에서 함수는 다음과 같은 함수와 다른 것을 반환하기 위해 반환 값을 호출 하고 변경 해야합니다 .
async function function_name(inStockItems, userObject, amount) {
const foundPurchases = await Purchase.find({
account: username,
fufilled: true
})
.populate("keys")
.exec();
if (foundPurchases.length === 0) {
return inStockItems;
} else {
let listOfReceivedIds = foundPurchases.keys.map(obj => obj.game);
for (let i = 0; i < inStockItems.length; i++) {
if (inStockItems.length <= minimum) {
return inStockItems;
}
let currentProductAnalysing = inStockItems[i];
if (listOfReceivedIds.includes(currentProductAnalysing._id)) {
console.log("removing product");
inStockItems.splice(i, 1);
}
}
return inStockItems;
}
}
또 다른 대안은 콜백 매개 변수를 함수에 전달하고 콜백에서 호출하는 exec()
것이지만 일반적으로 약속이 더 깔끔하게 작동합니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다