MongoDB没有保存用户

时间:2017-09-16 17:39:49

标签: mongodb mongoose database-schema

尝试为用户执行简单的CRUD。 我通过React / Redux的注册表格传递姓名,电子邮件,密码,位置,头像。

我在保存期间收到错误,其中显示以下关于重复键错误的内容 -

(node:30190) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): WriteError({"code":11000,"index":0,"errmsg":"E11000 duplicate key error index: heroku_10vr0vgl.users.$googleId_1 dup key: { : null }","op":{"avatar":"avatar.jpg","slug":"jane","id":"cj7nl39w50000amcaool50zcp","name":"Jane","email":"admin@jane.com","password":"$2a$10$envMKYq6xFCkZGpYVd4rEel4g5TGijFOEnr.ayMymHM1ph0/1luGC","location":"Los Angeles","_id":"59bd5e9478537875ee6b8939","createdAt":"2017-09-16T17:25:40.863Z","campaigns":[],"role":"NORMAL","__v":0}})

看起来是重复的val,但不需要googleID for ex。

我在用户集合中只有一个模拟用户,如下所示 -

[
  {
    "_id": {
      "$oid": "59a070dea4de3af502af7d5c"
    },
    "avatar": "avatar.jpg",
    "name": "John Doe",
    "role": "ADMIN",
    "slug": "john-doe",
    "id": "1",
    "email": "admin@johndoe.com",
    "location": "Los Angeles",
    "campaigns": ["1"],
    "twitterId": "refinery29",
    "password": "test1234"
  }
]

用户模型如下所示 -

import mongoose from 'mongoose';

const Schema = mongoose.Schema;

const userSchema = new Schema({
  name: {type: 'String', required: true},
  role: {type: 'String', default: 'NORMAL', required: true},
  location: {type: 'String'},
  slug: {type: 'String', required: true, index: {unique: true, dropDups: true}},
  campaigns: {type: ['String'], default: []},
  avatar: {type: 'String', required: true},
  email: {type: 'String', index: {unique: true, dropDups: true}},
  id: {type: 'String', required: true, index: {unique: true, dropDups: true}},
  googleId: {type: 'String'},
  facebookId: {type: 'String'},
  twitterId: {type: 'String'},
  createdAt: {type: 'Date', default: Date.now, required: true},
  password: {type: 'String'},
});

export default mongoose.model('User', userSchema);

1 个答案:

答案 0 :(得分:0)

看起来我有一个现有的索引引用。我删除了我的用户集并保存了。一切都很好!