从 mongoose 传递“_id”时得到空字符串

问题描述 投票:0回答:1

我正在使用 Node js、express 和 mongoose 创建一个待办事项列表应用程序。

现在我正在尝试删除项目功能,使用表单中的复选框将 item._id 传递给 Express,

我的待办事项列表项的数据类型是这样的:

const todoSchema = new mongoose.Schema({
    context: String
});


todoList> db.todolists.find()
[
  {
    _id: ObjectId("65085d9277578bec0a1b9415"),
    context: 'angry',
    __v: 0
  },
  {
    _id: ObjectId("65085db177578bec0a1b9421"),
    context: 'extremely angry',
    __v: 0
  },

这是我的EJS代码:

<form class="delete-form" action="/delete" method="POST">
    <div class="todobox">
        <input name="checkbox" value="<%=item._id%>" type="checkbox" onchange="this.form.submit()">
        <p>
            <%= item.context %>
        </p>
    </div>
</form>

这是我的 JS 代码:

app.post("/delete", async(req, res) => {
    const checkedItem = req.body.checkbox;
    console.log(checkedItem);
    todolist.deleteOne({_id: checkedItem})
    .then(console.log(`Item (${checkedItem}) was deleted.`)).catch((err) => {
         console.log(err);
     });
    items = await todolist.find({}, { _id: 0, context: 1 });
    console.log(`now the to-do-list has ${items.length} items.`)
    res.redirect("/");
});

我打印出 item._id ( req.body.checkbox )。只得到一个空字符串“”。
捕获错误:

CastError: Cast to ObjectId failed for value "" (type string) at path "_id" for model "todolist"
    at ObjectId.cast (/Users/xuxiang/Documents/BACKEND/todoList-one/node_modules/mongoose/lib/schema/objectid.js:250:11)
    at SchemaType.applySetters (/Users/xuxiang/Documents/BACKEND/todoList-one/node_modules/mongoose/lib/schematype.js:1220:12)
    at SchemaType.castForQuery (/Users/xuxiang/Documents/BACKEND/todoList-one/node_modules/mongoose/lib/schematype.js:1632:15)
    at cast (/Users/xuxiang/Documents/BACKEND/todoList-one/node_modules/mongoose/lib/cast.js:356:32)
    at Query.cast (/Users/xuxiang/Documents/BACKEND/todoList-one/node_modules/mongoose/lib/query.js:4911:12)
    at Query._castConditions (/Users/xuxiang/Documents/BACKEND/todoList-one/node_modules/mongoose/lib/query.js:2232:10)
    at model.Query._deleteOne (/Users/xuxiang/Documents/BACKEND/todoList-one/node_modules/mongoose/lib/query.js:3021:8)
    at model.Query.exec (/Users/xuxiang/Documents/BACKEND/todoList-one/node_modules/mongoose/lib/query.js:4430:28)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  stringValue: '""',
  messageFormat: undefined,
  kind: 'ObjectId',
  value: '',
  path: '_id',
  reason: BSONError: Argument passed in must be a string of 12 bytes or a string of 24 hex characters or an integer
      at new ObjectId (/Users/xuxiang/Documents/BACKEND/todoList-one/node_modules/bson/lib/bson.cjs:2055:23)
      at castObjectId (/Users/xuxiang/Documents/BACKEND/todoList-one/node_modules/mongoose/lib/cast/objectid.js:25:12)
      at ObjectId.cast (/Users/xuxiang/Documents/BACKEND/todoList-one/node_modules/mongoose/lib/schema/objectid.js:248:12)
      at SchemaType.applySetters (/Users/xuxiang/Documents/BACKEND/todoList-one/node_modules/mongoose/lib/schematype.js:1220:12)
      at SchemaType.castForQuery (/Users/xuxiang/Documents/BACKEND/todoList-one/node_modules/mongoose/lib/schematype.js:1632:15)
      at cast (/Users/xuxiang/Documents/BACKEND/todoList-one/node_modules/mongoose/lib/cast.js:356:32)
      at Query.cast (/Users/xuxiang/Documents/BACKEND/todoList-one/node_modules/mongoose/lib/query.js:4911:12)
      at Query._castConditions (/Users/xuxiang/Documents/BACKEND/todoList-one/node_modules/mongoose/lib/query.js:2232:10)
      at model.Query._deleteOne (/Users/xuxiang/Documents/BACKEND/todoList-one/node_modules/mongoose/lib/query.js:3021:8)
      at model.Query.exec (/Users/xuxiang/Documents/BACKEND/todoList-one/node_modules/mongoose/lib/query.js:4430:28),
  valueType: 'string',
  model: Model { todolist }
}

但是当我通过item.context来表达时,它可以成功得到正确的结果。对于_id和__v,我只能得到空字符串“”。

我也找了课程的讨论,问了ChatGPT,没有得到解决。

感谢您的时间和帮助。

javascript node.js mongodb mongoose ejs
1个回答
0
投票

感谢@nimrod serok。我改变了我的删除方法

app.post("/delete", async(req, res) => {
    ...
    items = await todolist.find({}, { _id: 0, context: 1 });
    ...
});

致:

app.post("/delete", async(req, res) => {
    ...
    items = await todolist.find({}, { _id: 1, context: 1 });
    ...
});

这影响了

req.body
,我现在可以获取_id了。 但是我仍然很困惑为什么会导致这个问题。

如果有人有想法请分享, 感谢@nimrod serok,非常感谢。

© www.soinside.com 2019 - 2024. All rights reserved.