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. 三栏布局只知道圣杯、双飞翼,最终评级是……F

    三栏布局,面试与工作中的常见布局.分左中右三部分,其中左右宽度已知,中间宽度自适应.根据不同的DOM顺序与CSS处理,这里写下了五类布局方式. 一.浮动布局 1 圣杯布局 L:"我问你,你就 ...

  2. Oracle11g新建用户及用户表空间

    /* 建立数据表空间 */CREATE TABLESPACE SP_TAB DATAFILE '/u01/app/oracle/oradata/orcl/tab1_1.dbf' size 1024M ...

  3. 解决如何通过循环来使用数据库的值设置jsp的select标签的option值

    Select 处的代码: <select name="position"> <span style="white-space:pre"> ...

  4. powerdesigner 15.1 逆向工程 sqlserver2008 、sqlserver2005 带注释

    第一种方法:在第一个网址里面的代码可以直接赋值到对应位置即可 http://wjqe.blog.163.com/blog/static/19938452011612536439/ 第二种方法:可塑性较 ...

  5. 【Elasticsearch】ElasticSearch基本查询

    学习elasticsearch查询用法的时候,发现这篇文章写得很详细,为以后方便查看,就直接搬过来了,原文链接在下面. 版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附 ...

  6. HDU 4812 (点分治)

    题目:https://vjudge.net/contest/307753#problem/E 题意:给你一颗树,树上每个点都有个权值,现在问你是否存在 一条路径的乘积 mod 1e6+3  等于 k的 ...

  7. [CSP-S模拟测试]:chinese(数学)

    题目传送门(内部题25) 输入格式 一行三个整数$n,m,k$. 输出格式 一行一个整数表示答案. 样例 样例输入: 2 2 2 样例输出: 数据范围与提示 样例解释: $f_0=10,f_1=4,f ...

  8. (转)Windows下安装Docker, GitBash环境配置

    转:https://blog.csdn.net/chengly0129/article/details/68944269 官网介绍: https://docs.docker.com/toolbox/t ...

  9. U-Boot是什么

    U-Boot U-Boot,全称 Universal Boot Loader,是遵循GPL条款的开放源码项目.U-Boot的作用是系统引导.U-Boot从FADSROM.8xxROM.PPCBOOT逐 ...

  10. 重新认识new

    前言 感谢大佬:https://www.cnblogs.com/luxiaoxun/archive/2012/08/10/2631812.html www.cplusplus.com 因为这段时间在重 ...