Я просто хочу update
данных в NewOrder Collection и затем выполнить некоторые операции с данными массива, но это возвращает ошибку ниже.
req.body contains the only array of _id of documents like [
'5e70bf169d79bb0e94633886', '5e70bf169d79bb0e94633888']`
TypeError: response.products.forEach is not a function
at C:\Users\Navin\Desktop\Angular Projects\node-rest-shop\api\routes\neworders.js:89:45
at processTicksAndRejections (internal/process/task_queues.js:97:5)
GET /ords/?page=1&limit=20 200 1606.706 ms - 5046
(node:1172) UnhandledPromiseRejectionWarning: MongooseServerSelectionError: getaddrinfo ENOTFOUND cluster0-shard-00-02-wv4bu.mongodb.net
at new MongooseServerSelectionError (C:\Users\Navin\Desktop\Angular Projects\node-rest-shop\node_modules\mongoose\lib\error\serverSelection.js:22:11)
at Function.Model.$wrapCallback (C:\Users\Navin\Desktop\Angular Projects\node-rest-shop\node_modules\mongoose\lib\model.js:4846:32)
at C:\Users\Navin\Desktop\Angular Projects\node-rest-shop\node_modules\mongoose\lib\query.js:4345:21
at C:\Users\Navin\Desktop\Angular Projects\node-rest-shop\node_modules\mongoose\lib\helpers\promiseOrCallback.js:31:5
at new Promise (<anonymous>)
at promiseOrCallback (C:\Users\Navin\Desktop\Angular Projects\node-rest-shop\node_modules\mongoose\lib\helpers\promiseOrCallback.js:30:10)
at model.Query.exec (C:\Users\Navin\Desktop\Angular Projects\node-rest-shop\node_modules\mongoose\lib\query.js:4344:10)
at C:\Users\Navin\Desktop\Angular Projects\node-rest-shop\api\routes\peginate.js:11:49
at Layer.handle [as handle_request] (C:\Users\Navin\Desktop\Angular Projects\node-rest-shop\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\Navin\Desktop\Angular Projects\node-rest-shop\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (C:\Users\Navin\Desktop\Angular Projects\node-rest-shop\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\Users\Navin\Desktop\Angular Projects\node-rest-shop\node_modules\express\lib\router\layer.js:95:5)
at C:\Users\Navin\Desktop\Angular Projects\node-rest-shop\node_modules\express\lib\router\index.js:281:22
at Function.process_params (C:\Users\Navin\Desktop\Angular Projects\node-rest-shop\node_modules\express\lib\router\index.js:335:12)
at next (C:\Users\Navin\Desktop\Angular Projects\node-rest-shop\node_modules\express\lib\router\index.js:275:10)
at Function.handle (C:\Users\Navin\Desktop\Angular Projects\node-rest-shop\node_modules\express\lib\router\index.js:174:3)`
Код, который создает эту ошибку:
router.post('/cancel/', (req, res, next) => {
const data = req.body;
console.log(data);
var tempArray = [];
data.forEach(order=> {
NewOrder.updateOne({_id: order}, { $set : {
order_status: "cancelled",
paid_amount: 0
}}).exec().then( result => {
NewOrder.findById(order)
.exec()
.then(response => {
response.products.forEach(product => {
let item = {
_id: product.prod_id,
quantity: product.req_quantity
}
tempArray.push(item);
});
})
.catch( err=> {
console.log(err);
});
})
.catch( err => {
console.log(err);
});
});
tempArray.forEach( element => {
console.log( element);
});
return res.status(200).json({
message: "Finished operation"
});
Может ли кто-нибудь мне помочь?