Node.js 无法通过匹配\u id更新MongoDB集合
首先,我用他的电子邮件和密码注册了一个用户 现在如果 我尝试在注册用户时通过匹配Mongo数据库分配的Id来更新或生成用户详细信息 它不接受它 错误是这样的Node.js 无法通过匹配\u id更新MongoDB集合,node.js,mongodb,mongoose,postman,Node.js,Mongodb,Mongoose,Postman,首先,我用他的电子邮件和密码注册了一个用户 现在如果 我尝试在注册用户时通过匹配Mongo数据库分配的Id来更新或生成用户详细信息 它不接受它 错误是这样的 要查找()的参数“filter”必须是一个对象,got 60b10821af9b63424cf427e8 如果我像这样解析它 Model.find(parseInt(req.params.id)) 它显示了一个不同的错误 这是邮寄申请 //Post request to create user details by matching Id.
要查找()的参数“filter”必须是一个对象,got 60b10821af9b63424cf427e8
如果我像这样解析它
Model.find(parseInt(req.params.id))
它显示了一个不同的错误
这是邮寄申请
//Post request to create user details by matching Id.
// Id I am trying to match is the id that was given by the database
app.post("/user/:id", async(req, res) => {
console.log("not found");
if (await Model.find(req.params.id)) {
const users = new Model({
fName: req.body.fName,
sName: req.body.sName,
birth: req.body.birth,
phone: req.body.phone,
SSN: req.body.SSN
});
const result = await users.save();
console.log(result);
return res.send({
"Success": true,
});
} else {
console.log(req.params.id);
res.status(404).send({ "message": false });
}
});
这是模式
const LoginSchema = new mongoose.Schema({
email: { type: String, required: true },
password: { type: String, required: false },
otp: String,
token: String,
fName: String,
sName: String,
birth: Number,
phone: Number,
SSN: Number
});
以下是我使用的标题
const express = require("express");
const app = express();
const mongoose = require("mongoose");
app.use(express.json());
app.use(express.urlencoded({ extend: true }));
错误很可能是由于您在Mongoose需要
Mongoose.Types.ObjectId
的实例时从请求中传递字符串造成的。您应该能够通过将字符串转换为所述类型来解决问题
await Model.find(mongoose.Types.ObjectId(req.params.id))
错误很可能是由于您在Mongoose需要
Mongoose.Types.ObjectId
的实例时从请求中传递字符串造成的。您应该能够通过将字符串转换为所述类型来解决问题
await Model.find(mongoose.Types.ObjectId(req.params.id))
错误:
Parameter "filter" to find() must be an object, got 60b10821af9b63424cf427e8
意思是:您需要提供一个对象作为find()
方法的参数,而不是字符串(本例中为“60b10821af9b63424cf427e8”)。此外,find()
将为您提供一个数组,如果您在数据库中找到一个项,请使用findOne()
更改为:
await Model.find(req.params.id)
致:
另一种方法是使用findById()
这样的方法:等待Model.findById(req.params.id)
错误:
Parameter "filter" to find() must be an object, got 60b10821af9b63424cf427e8
意思是:您需要提供一个对象作为find()
方法的参数,而不是字符串(本例中为“60b10821af9b63424cf427e8”)。此外,find()
将为您提供一个数组,如果您在数据库中找到一个项,请使用findOne()
更改为:
await Model.find(req.params.id)
致:
另一种方法是像这样使用findById()
方法:wait Model.findById(req.params.id)