MongoError:E11000重复键错误

时间:2017-04-02 02:49:53

标签: node.js mongodb

我正在使用nodejs + express创建一个简单的博客应用程序,我可以添加第一篇帖子没有问题但是当我尝试添加第二篇帖子时我得到了他的错误<script> function doSomething() { var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (xhttp.readyState == 4) { if (xhttp.status == 200) document.getElementById("calContent").innerHTML = xhttp.responseText; } } xhttp.open("GET", "/num/ns.php?number=" + jQuery("#nInput").val() + "$$$" + jQuery('input[name=switcher]:checked').val(), true); xhttp.send(); } function doSomethingElse() { var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (xhttp.readyState == 4) { if (xhttp.status == 200) document.getElementById("calContent").innerHTML = xhttp.responseText; } } xhttp.open("GET", "/num/ns.php?number2=" + jQuery("#nInput2").val() + "$$$" + jQuery('input[name=switcher2]:checked').val(), true); xhttp.send(); } </script> <style> .calOut { position: relative; float: left; margin-right: 18px; } </style> <style media="print"> #calContent { display: none; } </style> <div id="calContent" style="margin-top: 0px; margin-bottom: 50px; color:black;" class="contact_form" > <?php $numname = "number"; if (isset($_GET["number"])) { echo "<div><input type='text' id='nInput' name='nInput' value='" . explode("$$$", $_GET["number"])[0] . "' />"; echo "<br /><input type='radio' name='switcher' value='female' "; if (explode("$$$", $_GET["number"])[1] == "female") echo "checked"; echo ">Female&nbsp;&nbsp;"; echo "<input type='radio' name='switcher' value='male' " . (explode("$$$", $_GET["number"])[1] == "male" ? "checked" : "") . ">Male&nbsp;&nbsp;"; echo "<input type='radio' name='switcher' value='last' " . (explode("$$$", $_GET["number"])[1] == "last" ? "checked" : "") . ">Last name&nbsp;&nbsp;"; } else { echo "<div><input placeholder='Enter vibration' type='text' id='nInput' name='nInput' />"; echo "<br /><input type='radio' name='switcher' value='female' checked>Female&nbsp;&nbsp;<input type='radio' name='switcher' value='male' >Male&nbsp;&nbsp;"; echo "<input type='radio' name='switcher' value='last' />Last name"; } echo "<br /><input type='submit' id='nForm' value='Generate names' onclick='doSomething()' class='qbutton' style='margin-bottom: 45px;'/><br /></div>"; if (isset($_GET["number2"])) { echo "<div><input type='text' id='nInput2' name='nInput2' value='" . explode("$$$", $_GET["number2"])[0] . "' />"; echo "<br /><input type='radio' name='switcher2' value='female' "; if (explode("$$$", $_GET["number2"])[1] == "female") echo "checked"; echo ">Female&nbsp;&nbsp;"; echo "<input type='radio' name='switcher2' value='male' " . (explode("$$$", $_GET["number2"])[1] == "male" ? "checked" : "") . ">Male&nbsp;&nbsp;"; echo "<input type='radio' name='switcher2' value='last' " . (explode("$$$", $_GET["number2"])[1] == "last" ? "checked" : "") . ">Last name&nbsp;&nbsp;"; $numname = "number2"; } else { echo "<div><input placeholder='Enter vibration and number, eg. Lisa 17/8' type='text' id='nInput2' name='nInput' />"; echo "<br /><input type='radio' name='switcher2' value='female' checked>Female&nbsp;&nbsp;<input type='radio' name='switcher2' value='male' >Male&nbsp;&nbsp;"; echo "<input type='radio' name='switcher2' value='last' />Last name"; } echo "<br /><input type='submit' id='nForm2' value='Generate names' onclick='doSomethingElse()' class='qbutton' /><br /></div>"; //echo $_SERVER['HTTP_REFERER']; //echo "<br />"; //if ($_SERVER['REMOTE_ADDR'] == "93.176.86.170" || $_SERVER['REMOTE_ADDR'] == "86.52.165.122") // echo $_SERVER['REQUEST_URI']; $values = [ "a" => 1, "i" => 1, "j" => 1, "q" => 1, "y" => 1, "å" => 1, "á" => 1, "à" => 1, "b" => 2, "k" => 2, "r" => 2, "c" => 3, "g" => 3, "l" => 3, "s" => 3, "ç" => 3, "d" => 4, "m" => 4, "t" => 4, "e" => 5, "h" => 5, "n" => 5, "x" => 5, "é" => 5, "è" => 5, "è" => "5", "u" => 6, "v" => 6, "w" => 6, "æ" => 6, "o" => 7, "z" => 7, "ø" => 7, "ô" => 7, "ó" => 7, "ò" => 7, "ö" => 7, "f" => 8, "p" => 8]; ?> <br /> <?php //ini_set('memory_limit', '2048M'); error_reporting(0); ini_set("display_errors", 0); ini_set("log_errors", 0); ini_set("error_reporting", 0); if (isset($_GET[$numname])) { $namecount = 0; if (is_numeric(explode("/", explode("$$$", $_GET["number"])[0])[0])) { echo "<br />"; if (explode("$$$", $_GET[$numname])[1] == "male") $handle = fopen("./malenames.txt", "r"); else if (explode("$$$", $_GET[$numname])[1] == "female") $handle = fopen("./femalenames.txt", "r"); else $handle = fopen("./surnames.txt", "r"); $output = Array(); if ($handle) { while (($line = fgets($handle)) !== false) { $bVal = 0; for($j=0;$j<=strlen(utf8_decode($line));$j++) { $bVal += $values[substr(utf8_decode(mb_strtolower($line)),$j,1)]; } if ($bVal==explode("/", explode("$$$", $_GET[$numname])[0])[0] || explode("/", explode("$$$", $_GET[$numname])[0])[0] == (1+(($bVal-1)%9)) ) { //echo $line . "<br />"; $output[$namecount] = $line . " (" . $bVal . "/" . (1+(($bVal-1)%9)) . ")<br />"; $namecount++; } //echo $line . "<br />"; } fclose($handle); echo "<p id='numcount'>Found " . $namecount . " names</p>"; echo "<br />"; for ($i=0;$i<count($output);$i++) echo $output[$i]; unset($output); } } else if (is_numeric(explode(" ", explode("/", explode("$$$", $_GET[$numname])[0])[0])[1])) { $parm1 = explode(" ", explode("$$$", $_GET[$numname])[0]); $oldname = utf8_decode($parm1[0]); $num = explode("/", $parm1[1])[0]; //echo "<p>" . $parm1 . "</p>"; //echo "<p>" . $oldname . "</p>"; //echo "<p>" . $num . "</p>"; echo "<br />"; if (explode("$$$", $_GET[$numname])[1] == "male") $handle = fopen("./malenames.txt", "r"); else if (explode("$$$", $_GET[$numname])[1] == "female") $handle = fopen("./femalenames.txt", "r"); else $handle = fopen("./surnames.txt", "r"); $output = Array(); if ($handle) { while (($line = fgets($handle)) !== false) { $linex = $parm1[0] . mb_strtolower($line);//utf8_decode(mb_strtolower($oldname . mb_strtolower($line))); $bVal = 0; for($j=0;$j<=strlen(utf8_decode($linex));$j++) { $bVal += $values[substr(utf8_decode(mb_strtolower($linex)),$j,1)]; } if ($bVal==$num || $num == (1+(($bVal-1)%9)) ) { //echo $line . "<br />"; $output[$namecount] = $linex . " (" . $bVal . "/" . (1+(($bVal-1)%9)) . ")<br />"; $namecount++; } //echo $line . "<br />"; } fclose($handle); echo "<p id='numcount'>Found " . $namecount . " names</p>"; echo "<br />"; for ($i=0;$i<count($output);$i++) echo $output[$i]; unset($output); } } else { echo "<br />Error in input"; } echo "<div style='clear:both;'></div>"; } ?> </div> <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>

这是我的架构

{ MongoError: E11000 duplicate key error collection: restful_blog_app_v2.blogs index: username_1 dup key: { : null }

这是用户架构

var mongoose = require("mongoose");
var passportLocalMongoose = require("passport-local-mongoose");

var BlogSchema = new mongoose.Schema({
    title: String,
    image: String,
    body: String,
    created: {
        type: Date,
        default: Date.now
    },
    author: {
        id: {
            type: mongoose.Schema.Types.ObjectId,
            ref: "User"
        },
        username: String
    }
});

BlogSchema.plugin(passportLocalMongoose);
module.exports = mongoose.model("Blog", BlogSchema);

这是创建新的邮政路线

var mongoose = require("mongoose");
var passportLocalMongoose = require("passport-local-mongoose");

var UserSchema = new mongoose.Schema({
    username: String,
    password: String,
});

UserSchema.plugin(passportLocalMongoose);

module.exports = mongoose.model("User", UserSchema);

我试图从mongo控制台使用db.dropDatabase()删除整个数据库,但问题仍然存在,不知道现在该怎么做

2 个答案:

答案 0 :(得分:1)

这是由passport-local-mongoose引起的,username根据its fine manual默认使BlogSchema成为唯一字段。

您已将此插件添加到UserSchema,这似乎您最初在该架构中拥有用户数据但将其移至单独的架构(BlogSchema)并忘记了把它从前者中移除。

首先不要将其用于username,您还需要删除blogs集合{{1}}上的唯一索引。

答案 1 :(得分:0)

您可以尝试删除架构并再次发送值吗?我遇到了同样的问题。我解决了以上想法。