node 添加个人经历的接口
1.定义experience
const profileFields = {};
profileFields.experience=[];
2.查找用户id
const profile = await Profile.find({ user: ctx.state.user.id });
3.判断用户的数据是否存在,
如果存在,就设计数据的结构,然后添进Profile 中,最后更新进去数据库里面
if(profile.length>) {
//设计experience 的结构数据
const newExp={
title: ctx.request.body.title,
current: ctx.request.body.current,
company: ctx.request.body.company,
location: ctx.request.body.location,
from: ctx.request.body.from,
to: ctx.request.body.to,
description: ctx.request.body.description
}
profileFields.experience.unshift(newExp);
//console.log(profileFields);
//在从前端添加数据后,我们更新数据到数据库
const profileUpdate = await Profile.findOneAndUpdate(
{
user:ctx.state.user.id
},
{
$set:profileFields
},
{
new:true
}
);
ctx.body=profileUpdate;
更新数据库
const profileUpdate = await Profile.findOneAndUpdate(
{
user:ctx.state.user.id
},
{
$set:profileFields
},
{
new:true
}
数据不存在
errors.noprofile = '没有该用户的信息';
ctx.status = 404;
ctx.body = errors
代码
/**
* @route post api/profile/experience
* @desc 工作经历接口
* @access 接口是私密的
*/
// localhost:4000/api/profile/experience
router.post('/experience', passport.authenticate('jwt', { session: false }), async ctx => {
//验证
const { errors, isValid } = validateExperienceInput(ctx.request.body);
//判断是否验证通过
if (!isValid) {
ctx.status = 400;
ctx.body = errors;
return;
}
const profileFields = {};
profileFields.experience=[];
//查找用户id是否存在
const profile = await Profile.find({ user: ctx.state.user.id });
//判断profile 的数据是否存在,存在就添加个人经历的数据
if(profile.length>0) {
//设计experience 的结构数据
const newExp={
title: ctx.request.body.title,
current: ctx.request.body.current,
company: ctx.request.body.company,
location: ctx.request.body.location,
from: ctx.request.body.from,
to: ctx.request.body.to,
description: ctx.request.body.description
}
profileFields.experience.unshift(newExp);
//console.log(profileFields);
//在从前端添加数据后,我们更新数据到数据库
const profileUpdate = await Profile.findOneAndUpdate(
{
user:ctx.state.user.id
},
{
$set:profileFields
},
{
new:true
}
);
ctx.body=profileUpdate; }else {
errors.noprofile = '没有该用户的信息';
ctx.status = 404;
ctx.body = errors;
} });
截图

node 添加个人经历的接口的更多相关文章
- Node教程——API接口开发(Node版的CRUD通用接口的搭建)(MangoDB+Express_Version2)
1. 概述 时间跨度有点大,之前就跟大家唠嗑过一些知识点,也开启了一个Node书写一个后台api项目的开始,出于各种原因,迟迟没有更新博文.现在我把这个坑填上,如果你还有阅读过我之前的文章,我建议你先 ...
- Android中添加监听回调接口的方法
在Android中,我们经常会添加一些监听回调的接口供别的类来回调,比如自定义一个PopupWindow,需要让new这个PopupWindow的Activity来监听PopupWindow中的一些组 ...
- Spring boot 添加日志 和 生成接口文档
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring- ...
- [译]WPF MVVM 架构 Step By Step(5)(添加actions和INotifyPropertyChanged接口)
应用不只是包含textboxs和labels,还包含actions,如按钮和鼠标事件等.接下来我们加上一些像按钮这样的UI元素来看MVVM类怎么演变的.与之前的UI相比,这次我们加上一个"C ...
- Node+express实现后台服务接口
一.准备工作 创建代码目录,依次执行以下操作 1.(若没有安装过)安装node 2.npm init(package.json) 3.安装express(请求)npm install express ...
- 小程序动态添加class及调接口传递多个参数
1.动态添加class <view class="step2 {{indication == 2 ?'on':''}}"> <view class='tc lef ...
- Node.js中的模块接口module.exports浅析
在写node.js代码时,我们经常需要自己写模块(module).同时还需要在模块最后写好模块接口,声明这个模块对外暴露什么内容.实际上,node.js的模块接口有多种不同写法.这里作者对此做了个简单 ...
- 使用navicat for mysql图形界面操作数据库、使用node.js操作数据库写接口
1.先启动MYSQL服务 2.打开navicat for mysql, 点击链接,输入如下的内容: 3.新建数据表 4.数据库(新建一个db.js) //数据库链接配置 module.exports ...
- element中upload单图片转base64后添加进数组,请求接口
//先上代码 <template> <!-- data绑定的参数 getuploadloge: [ { url: '', name: '' } ], --> <!-- 编 ...
随机推荐
- 牛客网NOIP赛前集训营-提高组(第七场)A-中国式家长 2
题目描述 有一天,牛牛找到了一个叫<中国式家长>的游戏,游戏中需要靠"挖脑洞"来提升悟性. 挖脑洞在一个\(N\)行\(M\)列的地图上进行,一开始牛牛有\(K\)点行 ...
- golang-练习3
题目:将输入的字母变成其下一个字母,并且元音字母大写 package main import "fmt" func LetterChanges(str string) string ...
- hadoop中mapreduce的mapper抽象类和reduce抽象类
mapreduce过程key 和value分别存什么值 https://blog.csdn.net/csdnliuxin123524/article/details/80191199 Mapper抽象 ...
- onkeyup的使用(将输入值为非数字的字符替换为空)
onkeyup:当输入值的键盘抬起时触发这个事件. 例如: onkeyup="this.value=this.value.replace(/\D/g,'') 这是个正则式验证,用来验证输入值 ...
- 【leetcode】331. Verify Preorder Serialization of a Binary Tree
题目如下: One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null ...
- 一分钟小知识:scroll-behavior 让你的页面导航滚动更丝滑~
中午在[掘金]潜水摸鱼,看到这一个沸点,个人已经撸出特效: 下面放上 作者 的 掘金 地址 #掘金沸点# https://juejin.im/pin/5d649eaaf265da19752533d ...
- vue中路由传参的方式
一.params的类型: 配置路由格式: /router/:id 传递的方式: 在path后面跟上对应的值 传递后形成的路径: /router/123, /router/abc 通过:to字符串拼接的 ...
- create-react-app 配置 修改
1.端口号修改:https://www.jianshu.com/p/80a7603dda70(亲测有效) 在 根据 package.json 的启动,node_modules文件夹里面搜索reac ...
- Linux awk 命令 说明
一. AWK 说明 awk是一种编程语言,用于在linux/unix下对文本和数据进行处理.数据可以来自标准输入.一个或多个文件,或其它命令的输出.它支持用户自定义函数和动态正则表达式等先进功能,是l ...
- java并发编程笔记(九)——多线程并发最佳实践
java并发编程笔记(九)--多线程并发最佳实践 使用本地变量 使用不可变类 最小化锁的作用域范围 使用线程池Executor,而不是直接new Thread执行 宁可使用同步也不要使用线程的wait ...