上一节写数据模型的时候我们已经知道了信息有:type、describe、income、expend、cash、remark、date,所以本节写添加信息接口也是需要这些类型的数据。
1、在profile.js里面写添加信息的接口。由于添加之前得判断是否登录着的,所以需要验证token,第二个参数我们需要验证
// $route POST api/profiles/add
// @desc 创建信息接口 ,第二个参数会验证我们的token
// @access Private
router.post("/add",passport.authenticate("jwt", {session: false}),(req,res)=>{
const profileFields = {};
if(req.body.type) profileFields.type = req.body.type;
if(req.body.describe) profileFields.describe = req.body.describe;
if(req.body.income) profileFields.income = req.body.income;
if(req.body.expend) profileFields.expend = req.body.expend;
if(req.body.cash) profileFields.cash = req.body.cash;
if(req.body.remark) profileFields.remark = req.body.remark;
new Profile(profileFields).save().then(profile =>{
res.json(profile);
});
});
2、验证是否可以添加
首先得先把测试登录接口时的token复制到添加接口这边来。

然后再填写各项信息进行send

测试结果:


注意:我用的是本地的mongodb,也可以用mlab(mlab已被mongo收购,具体怎么使用自己去研究吧。)
网友评论