我正在嘗試使用 express 和 MongoDb 實作簡單的注冊驗證,但下面的代碼行總是失敗并出現以下錯誤
const emailExist = await User.findOne({email: req.body.email});
錯誤
_\node_modules\mongodb\lib\cmap\connection.js:203
callback(new error_1.MongoServerError(document))
^
MongoServerError: Expected field collationto be of type object
at Connection.onMessage (C:\Users\moham\Desktop\NetRe_\node_modules\mongodb\lib\cmap\connection.js:203:30)
at MessageStream.<anonymous> (C:\Users\moham\Desktop\NetRe_\node_modules\mongodb\lib\cmap\connection.js:63:60)
at MessageStream.emit (node:events:394:28)
at processIncomingData (C:\Users\moham\Desktop\NetRe_\node_modules\mongodb\lib\cmap\message_stream.js:108:16)
at MessageStream._write (C:\Users\moham\Desktop\NetRe_\node_modules\mongodb\lib\cmap\message_stream.js:28:9)
at writeOrBuffer (node:internal/streams/writable:389:12)
at _write (node:internal/streams/writable:330:10)
at MessageStream.Writable.write (node:internal/streams/writable:334:10)
at TLSSocket.ondata (node:internal/streams/readable:754:22)
at TLSSocket.emit (node:events:394:28) {
ok: 0,
code: 14,
codeName: 'TypeMismatch',
'$clusterTime': {
clusterTime: Timestamp { low: 25, high: 1650230278, unsigned: true },
signature: {
hash: Binary {
sub_type: 0,
buffer: Buffer(20) [Uint8Array] [
74, 137, 251, 211, 139, 236,
64, 99, 37, 21, 24, 232,
160, 41, 22, 158, 46, 34,
97, 169
],
position: 20
},
keyId: Long { low: 2, high: 1641040568, unsigned: false }
}
},
operationTime: Timestamp { low: 25, high: 1650230278, unsigned: true },
[Symbol(errorLabels)]: Set(0) {}
}
用戶模型結構
const mongoose = require ('mongoose')
const userSchema = new mongoose.Schema({
firstName: {type: String, required:true},
lastName: {type: String, required:true},
date: {type: Date},
id: {type: String, required:true, unique: true},
idType: {type: String, required:true},
gender: {type: String, required:true},
mobile: {type: String, required:true, unique: true},
email: {type: String, required:true},
password: {type: String, required:true, max: 1024, min: 6}
},
{ collation: 'user-data'})
const model = mongoose.model ('User', userSchema)
module.exports = model
快遞服務器代碼
router.post('/register', async (req,res)=>{
//validate data before insert
const { error } = registerValidation(req.body);
if(error) {
return res.status(400).send(error.details[0].message)
}
//Checking if the user is already int the database
const emailExist = await User.findOne({email: req.body.email});
if(emailExist) {
return res.status(400).send('Email already exists')
}
//Create new user
const user= new User(req.body);
try {
const savedUser = await user.save();
res.send(savedUser)
} catch (error) {
res.status(400).send(err);
}
})
module.exports = router;
uj5u.com熱心網友回復:
正如錯誤所述,它正在尋找collation一個物件,你將它作為一個字串。我不確定您user-data將排序規則中的字串用于什么目的。根據檔案:
排序規則允許用戶為字串比較指定特定于語言的規則,例如字母大小寫和重音符號的規則。
因此,為了使用它,該locale欄位是強制性的。如果您確定要對此模式進行排序,最簡單的方法是將模式更改為:
const userSchema = new mongoose.Schema({
firstName: {type: String, required:true},
lastName: {type: String, required:true},
date: {type: Date},
id: {type: String, required:true, unique: true},
idType: {type: String, required:true},
gender: {type: String, required:true},
mobile: {type: String, required:true, unique: true},
email: {type: String, required:true},
password: {type: String, required:true, max: 1024, min: 6}
},
{ collation: { locale: 'en_US', strength: 1 } })
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/460145.html
標籤:javascript 节点.js mongodb 猫鼬
