猫鼬 findByIdAndDelete/findOneAndRemove 不删除 [英] mongoose findByIdAndDelete / findOneAndRemove not deleting

查看:263
本文介绍了猫鼬 findByIdAndDelete/findOneAndRemove 不删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用基本表单从 mongodb 创建/删除信息.当我提交表单时,我可以创建足够好的记录,但是当我尝试删除记录时,我不断收到此错误:

Im using a basic forms to create/delete info from mongodb. I can create the records fine enough when i submit the form but when i try to remove a record i keep getting this error:

     Cast to ObjectId failed for value " 596f5d7770f6f0d0c2767d3f" at 
path "_id" for model "dwb_notes"

我环顾四周,我在这里看到的大多数答案都涉及运行应用程序的多个实例或在同一端口上运行的多个程序等,以及与我的问题没有直接关系的其他各种解决方案(我认为)但我已经尝试过它们无济于事.

I've looked around and most of the answers i see here on SO speak to multiple instances of the app running or multiple programs running on the same port etc, and other varied solutions that aren't directly related to my problem(i think) but i've tried them anyways to no avail.

我现在在数据库中只有一条记录用于测试.这是我的模型:

I only have one record in the db right now for testing. Here is my model:

function todoNotesModel(){
    const mongoose = require("mongoose");
    const Schema = mongoose.Schema;

    const NotesSchema = new Schema({
        todoCompany : String,
        todoIsFor : String, 
        todoPostedBy : String,
        todoCompleted : Boolean,
        todoPriority : String,
        todoMessages : Array,
        todoTitle : String,    
        todoDate : String
    });

    const NotesModel = mongoose.model("dwb_notes", NotesSchema);

    return NotesModel;
}

module.exports = todoNotesModel();

使用另一个 html 表单,在加载时,删除按钮被分配文档 ID (在本例中为 596f5d7770f6f0d0c2767d3f),并将其附加到删除按钮.提交时,此 id 将通过以下方式传递给删除路由:

Using another html form, on load, the delete button is assigned the document id (in this case its 596f5d7770f6f0d0c2767d3f ), and appends it to the delete button. on submit, this id is passed to the delete route via:

app.delete("/notesapi/:tabID", (request, response) => {
    NotesModel.findByIdAndRemove(request.params.tabID, (error, data)=>{
        if(error){
            console.log("error in deleting yo!");
            throw error;
        } else {
            console.log("data all gone and deleted yo");
            response.status(204);

        }
    });
});

当我运行一个:db.dwb_notes.find().pretty()

我回来了:

"_id" : ObjectId("596f5d7770f6f0d0c2767d3f"),
"todoCompany" : "",
"todoIsFor" : "",
"todoPostedBy" : "LOGGED IN USER HERE",
"todoCompleted" : false,
"todoPriority" : "green",
"todoTitle" : "",
"todoDate" : "Wed Jul 19 2017 09:23:56 GMT-0400 (Eastern Daylight Time)",
"todoMessages" : [
        ""
],
"__v" : 0

它显示在那里,所以我不明白为什么我会收到这个错误.有什么想法吗?

which shows its there so i don't understand why i'm getting this error. Any ideas?

推荐答案

看起来您的错误是在您的 MongoID 字符串中打印出一个额外的空间.创建无效的 MongoID 字符串.

it looks like your error is printing out an additional space in your MongoID String. Creating an invalid MongoID String.

您可以使用:string.trim() 文档

删除字符串前后的任何空格.

To remove any spaces before or after the string.

这篇关于猫鼬 findByIdAndDelete/findOneAndRemove 不删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆