异步函数未保存在数据库中
Posted
技术标签:
【中文标题】异步函数未保存在数据库中【英文标题】:Async function not saving in database 【发布时间】:2019-12-12 11:31:45 【问题描述】:这个函数应该将购物车状态保存到我的数据库中。 好吧,我想我应该调用 await Axios .post 将数据保存在我的数据库中,但我不知道该怎么做,我已经搜索了一个小时 这是在客户端,后端的邮递员一切正常
sendCart = async cart =>
const config =
headers:
'Content-Type': 'application/json'
;
try
const res = await axios.post('/api/purchase', cart, config)
catch (err)
console.error(err)
typeError: req.body.map is not a function
at router.post (C:\Users\Test\Documents\GitHub\Ecommerce-Store\VBackend\routes\Purchase.js:7:24)
at Layer.handle [as handle_request] (C:\Users\Test\Documents\GitHub\Ecommerce-Store\VBackend\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\Test\Documents\GitHub\Ecommerce-Store\VBackend\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (C:\Users\Test\Documents\GitHub\Ecommerce-Store\VBackend\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\Users\Test\Documents\GitHub\Ecommerce-Store\VBackend\node_modules\express\lib\router\layer.js:9
MongoDb Connected
TypeError: req.body.map is not a function
at router.post (C:\Users\Test\Documents\GitHub\Ecommerce-Store\VBackend\routes\Purchase.js:7:24)
at Layer.handle [as handle_request] (C:\Users\Test\Documents\GitHub\Ecommerce-Store\VBackend\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\Test\Documents\GitHub\Ecommerce-Store\VBackend\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (C:\Users\Test\Documents\GitHub\Ecommerce-Store\VBackend\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\Users\Test\Documents\GitHub\Ecommerce-Store\VBackend\node_modules\express\lib\router\layer.js:95:5)
at C:\Users\Test\Documents\GitHub\Ecommerce-Store\VBackend\node_modules\express\lib\router\index.js:281:22
at Function.process_params (C:\Users\Test\Documents\GitHub\Ecommerce-Store\VBackend\node_modules\express\lib\router\index.js:335:12)
at next (C:\Users\Test\Documents\GitHub\Ecommerce-Store\VBackend\node_modules\express\lib\router\index.js:275:10)
at Function.handle (C:\Users\Test\Documents\GitHub\Ecommerce-Store\VBackend\node_modules\express\lib\router\index.js:174:3)
at router (C:\Users\Test\Documents\GitHub\Ecommerce-Store\VBackend\node_modules\express\lib\router\index.js:47:12)
at Layer.handle [as handle_request] (C:\Users\Test\Documents\GitHub\Ecommerce-Store\VBackend\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (C:\Users\Test\Documents\GitHub\Ecommerce-Store\VBackend\node_modules\express\lib\router\index.js:317:13)
at C:\Users\Test\Documents\GitHub\Ecommerce-Store\VBackend\node_modules\express\lib\router\index.js:284:7
at Function.process_params (C:\Users\Test\Documents\GitHub\Ecommerce-Store\VBackend\node_modules\express\lib\router\index.js:335:12)
at next (C:\Users\Test\Documents\GitHub\Ecommerce-Store\VBackend\node_modules\express\lib\router\index.js:275:10)
at jsonParser (C:\Users\Test\Documents\GitHub\Ecommerce-Store\VBackend\node_modules\body-parser\lib\types\json.js:119:7)
TypeError: req.body.map is not a function
at router.post (C:\Users\Test\Documents\GitHub\Ecommerce-Store\VBackend\routes\Purchase.js:7:24)
at Layer.handle [as handle_request] (C:\Users\Test\Documents\GitHub\Ecommerce-Store\VBackend\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\Test\Documents\GitHub\Ecommerce-Store\VBackend\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (C:\Users\Test\Documents\GitHub\Ecommerce-Store\VBackend\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\Users\Test\Documents\GitHub\Ecommerce-Store\VBackend\node_modules\express\lib\router\layer.js:95:5)
at C:\Users\Test\Documents\GitHub\Ecommerce-Store\VBackend\node_modules\express\lib\router\index.js:281:22
at Function.process_params (C:\Users\Test\Documents\GitHub\Ecommerce-Store\VBackend\node_modules\express\lib\router\index.js:335:12)
at next (C:\Users\Test\Documents\GitHub\Ecommerce-Store\VBackend\node_modules\express\lib\router\index.js:275:10)
at Function.handle (C:\Users\Test\Documents\GitHub\Ecommerce-Store\VBackend\node_modules\express\lib\router\index.js:174:3)
at router (C:\Users\Test\Documents\GitHub\Ecommerce-Store\VBackend\node_modules\express\lib\router\index.js:47:12)
at Layer.handle [as handle_request] (C:\Users\Test\Documents\GitHub\Ecommerce-Store\VBackend\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (C:\Users\Test\Documents\GitHub\Ecommerce-Store\VBackend\node_modules\express\lib\router\index.js:317:13)
at C:\Users\Test\Documents\GitHub\Ecommerce-Store\VBackend\node_modules\express\lib\router\index.js:284:7
at Function.process_params (C:\Users\Test\Documents\GitHub\Ecommerce-Store\VBackend\node_modules\express\lib\router\index.js:335:12)
at next (C:\Users\Test\Documents\GitHub\Ecommerce-Store\VBackend\node_modules\express\lib\router\index.js:275:10)
at jsonParser (C:\Users\Test\Documents\GitHub\Ecommerce-Store\VBackend\node_modules\body-parser\lib\types\json.js:119:7)
这是我的路线
const express = require("express");
const models = require("../models/Cart");
const router = express.Router();
router.post("/", (req, res) =>
const newPurchase = new models.cartModel(
products: req.body.map(element =>
const colorC, sizeC, date, title, transactionID, count = element;
return colorC, sizeC, date, title, transactionID, count ;
)
);
newPurchase
.save()
.then(purchase => res.json(purchase))
.catch(err =>
console.error(err.message);
res.status(500).send("Server Error");
);
);
module.exports = router;
【问题讨论】:
你能更好地解释你的问题吗?添加有关您的问题的更多详细信息,并向我们解释您想要的结果是什么。这将极大地帮助我们试图解决您的问题。谢谢! 我没有收到错误,只是没有保存在数据库中 在教程中我正在使用减速器,但我不想使用这个我只想从函数中调用有效负载 此代码来自客户端。服务器端代码在哪里?在服务器中是实际数据库写入发生的地方。像这样的东西:thepolyglotdeveloper.com/2019/02/… 服务器进程,打印或记录传入的请求以查看值 【参考方案1】:嘿,所以我实际上并没有正确地通过函数传递购物车数据,所以一开始一切都是正确的。
sendCart = async cart =>
const config =
headers:
'Content-Type': 'application/json'
try
const res = await axios.post('/api/purchase',cart, config)
catch (err)
console.error(err)
【讨论】:
您能否详细说明您的代码与问题中的代码有何不同?你的代码是如何解决问题的?以上是关于异步函数未保存在数据库中的主要内容,如果未能解决你的问题,请参考以下文章