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 添加个人经历的接口的更多相关文章

  1. Node教程——API接口开发(Node版的CRUD通用接口的搭建)(MangoDB+Express_Version2)

    1. 概述 时间跨度有点大,之前就跟大家唠嗑过一些知识点,也开启了一个Node书写一个后台api项目的开始,出于各种原因,迟迟没有更新博文.现在我把这个坑填上,如果你还有阅读过我之前的文章,我建议你先 ...

  2. Android中添加监听回调接口的方法

    在Android中,我们经常会添加一些监听回调的接口供别的类来回调,比如自定义一个PopupWindow,需要让new这个PopupWindow的Activity来监听PopupWindow中的一些组 ...

  3. Spring boot 添加日志 和 生成接口文档

    <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring- ...

  4. [译]WPF MVVM 架构 Step By Step(5)(添加actions和INotifyPropertyChanged接口)

    应用不只是包含textboxs和labels,还包含actions,如按钮和鼠标事件等.接下来我们加上一些像按钮这样的UI元素来看MVVM类怎么演变的.与之前的UI相比,这次我们加上一个"C ...

  5. Node+express实现后台服务接口

    一.准备工作 创建代码目录,依次执行以下操作 1.(若没有安装过)安装node 2.npm init(package.json) 3.安装express(请求)npm install express ...

  6. 小程序动态添加class及调接口传递多个参数

    1.动态添加class <view class="step2 {{indication == 2 ?'on':''}}"> <view class='tc lef ...

  7. Node.js中的模块接口module.exports浅析

    在写node.js代码时,我们经常需要自己写模块(module).同时还需要在模块最后写好模块接口,声明这个模块对外暴露什么内容.实际上,node.js的模块接口有多种不同写法.这里作者对此做了个简单 ...

  8. 使用navicat for mysql图形界面操作数据库、使用node.js操作数据库写接口

    1.先启动MYSQL服务 2.打开navicat for mysql, 点击链接,输入如下的内容: 3.新建数据表 4.数据库(新建一个db.js) //数据库链接配置 module.exports ...

  9. element中upload单图片转base64后添加进数组,请求接口

    //先上代码 <template> <!-- data绑定的参数 getuploadloge: [ { url: '', name: '' } ], --> <!-- 编 ...

随机推荐

  1. Flutter日曆國際化

    Flutter自带的日期选择器是showDatePicker,时间选择器是showTimePicker. 这两个选择器默认的显示效果都是英文的,我们是在中国,那么就需要将其显示成中文版的,这就涉及到F ...

  2. Codeforces Round #518 (Div. 1) Computer Game 倍增+矩阵快速幂

    接近于死亡的选手没有水平更博客,所以现在每五个月更一篇. 这道题呢,首先如果已经有权限升级了,那么后面肯定全部选的是 \(p_ib_i\) 最高的. 设这个值为 \(M=\max \limits_i ...

  3. 8.xpath(dom4j支持的jar)

    1.使用dom4j支持xpath的操作(xpath可以直接获取到某个元素) (1)第一种形式 /AAA/DDD/BBB:表示一层一层的,AAA下面DDD下面的BBB元素 (2)第二种形式 //BBB: ...

  4. 人生苦短_我用Python_javascript_var_function_简单笔记_001

    <!--Javascript_var_001:--> <html> <head> <meta charset="UTF-8"> &l ...

  5. Angular JS - 5 - Angular JS 模块和控制器

    1.引入 1.5版本的angularjs,直接打印angular对象: --> <!DOCTYPE html> <html> <head lang="en ...

  6. 浅析弹性公网IP付费模式和短时升配功能介绍

    ​ 弹性公网IP付费模式对比 弹性公网IP(EIP),有两种付费方式.一种是预付费,一种是后付费.对于预付费弹性公网IP而言,最大的优点就是带宽费用便宜,相对于后付费有比较大的优惠. 例如,杭州地域6 ...

  7. Yii2 $app总结

    //验证登陆 Yii::$app->user->isGuest; //当前user的id Yii::$app->user->id; //当前controller的名称 Yii: ...

  8. 各大漏洞平台及SRC的区别和如何批量刷漏洞

    批量刷漏洞: 01刷指纹->02刷原始漏洞->03刷CMS->04刷指定政府.教育->05刷众测平台->06刷SRC->07刷国内外.活动 搜索引擎: 百度.goo ...

  9. PHP curl_error函数

    curl_error — 返回一个保护当前会话最近一次错误的字符串 说明 string curl_error ( resource $ch ) 返回一条最近一次cURL操作明确的文本的错误信息. 参数 ...

  10. [CSP-S模拟测试]:次芝麻(数学)

    题目描述 小$K$和小$X$都是小次货.身为小次货,最重要的事情就是次啦!所以他们正在纠结如何分芝麻次.一开始,小$K$有$n$个芝麻,小$X$有$m$个芝麻.因为他们都想次更多芝麻,所以每次手中芝麻 ...