javascript - 转换为 ObjectId 失败
问题描述
我想使用 findById 将一个字段放入我的数据库,但是当我运行它时,我遇到了这个错误
“转换为 ObjectId 值失败”查询
{\n _mongooseOptions: {},\n _transforms: [],\n _hooks: Kareem { _pres: Map {}, _posts: Map {} },\n _executionCount: 0,\n mongooseCollection:\n NativeCollection {\n collection: Collection { s: [Object] },\n Promise: [Function: Promise],\n opts:\n { bufferCommands: true,\n capped: false,\n Promise: [Function: Promise],\n '$wasForceClosed': undefined },\n name: 'etudiants',\n collectionName: 'etudiants',\n conn:\n NativeConnection {\n base: [Mongoose],\n collections: [Object],\n models: [Object],\n config: [Object],\n replica: false,\n options: null,\n otherDbs: [],\n relatedDbs: {},\n states: [Object],\n _readyState: 1,\n _closeCalled: false,\n _hasOpened: true,\n plugins: [],\n id: 0,\n _listening: false,\n _connectionOptions: [Object],\n name: 'Ndao',\n host: 'localhost',\n port: 27017,\n user: 'root',\n pass: undefined,\n client: [MongoClient],\n '$initialConnection': [Promise],\n _events: [Object],\n _eventsCount: 1,\n db: [Db] },\n queue: [],\n buffer: false,\n emitter:\n EventEmitter { _events: {}, _eventsCount: 0, _maxListeners: undefined } },\n model: Model { etudiant },\n schema:\n Schema {\n obj:\n { nom: [Object],\n prenom: [Object],\n classe: [Object],\n etablissement: [Object],\n telephone: [Object],\n email: [Object],\n password: [Object],\n courSuivi: [Array] },\n paths:\n { nom: [SchemaString],\n prenom: [SchemaString],\n classe: [SchemaString],\n etablissement: [SchemaString],\n telephone: [SchemaString],\n email: [SchemaString],\n password: [SchemaString],\n courSuivi: [SchemaArray],\n _id: [ObjectId],\n __v: [SchemaNumber] },\n aliases: {},\n subpaths: { 'courSuivi.$': [ObjectId] },\n virtuals: { id: [VirtualType] },\n singleNestedPaths: {},\n nested: {},\n inherits: {},\n callQueue: [],\n _indexes: [],\n methods: { comparePassword: [Function: comparePassword] },\n methodOptions: {},\n statics: {},\n tree:\n { nom: [Object],\n prenom: [Object],\n classe: [Object],\n etablissement: [Object],\n telephone: [Object],\n email: [Object],\n password: [Object],\n courSuivi: [Array],\n _id: [Object],\n __v: [Function: Number],\n id: [VirtualType] },\n query: {},\n childSchemas: [],\n plugins: [ [Object], [Object], [Object], [Object], [Object] ],\n '$id': 1,\n s: { hooks: [Kareem] },\n _userProvidedOptions: {},\n options:\n { typePojoToMixed: true,\n typeKey: 'type',\n id: true,\n noVirtualId: false,\n _id: true,\n noId: false,\n validateBeforeSave: true,\n read: null,\n shardKey: null,\n autoIndex: null,\n minimize: true,\n discriminatorKey: '__t',\n versionKey: '__v',\n capped: false,\n bufferCommands: true,\n strict: true,\n pluralization: true },\n '$globalPluginsApplied': true },\n op: 'findOne',\n options: {},\n _conditions: { _id: '5e57814484f3e0040ce5358b' },\n _fields: undefined,\n _update: undefined,\n _path: undefined,\n _distinct: undefined,\n _collection:\n NodeCollection {\n collection:\n NativeCollection {\n collection: [Collection],\n Promise: [Function: Promise],\n opts: [Object],\n name: 'etudiants',\n collectionName: 'etudiants',\n conn: [NativeConnection],\n queue: [],\n buffer: false,\n emitter: [EventEmitter] },\n collectionName: 'etudiants' },\n _traceFunction: undefined,\n '$useProjection': true }\" at path \"etudiants\""
这是我的代码
CoursSchema.statics.addEtudiant = function(idCours, idEtudiant) {
const Etudiant = mongoose.model('etudiant');
const etudiant = Etudiant.findOne({_id: idEtudiant});
console.log(idEtudiant);
/* const Cours = mongoose.model('cours'); */
return this.findById(idCours)
.then(cours => {
/* console.log(cours); */
console.log(etudiant);
if(cours.etudiants == null) {
cours.etudiants = [];
cours.etudiants.push(etudiant);
} else {
cours.etudiants.push(etudiant);
}
/* const updateCours = Cours.findOneAndUpdate({_id:cours._id },{etudiants: cours.etudiant}) */
return Promise.all([cours.save()])
.then(([etudiant, cours]) => cours);
/* return Promise.all([etudiant.save(), cours.save()])
.then(([etudiant, cours]) => cours); */
});
}
解决方案
推荐阅读
- .git-folder - 在我的存储库中找不到 .git 文件夹
- android - 无法实例化一个或多个类(Recyclerview 不工作)
- sql - 将行扫描成切片
- datadog - Datadog - 按日志子串分组
- java - 如何知道在 android 中使用的权限列表?
- java - 设置 Google AppEngine Jetty 版本
- c++ - 无法获得菜单选择以运行功能。C++
- python - 为什么 ImportError:没有名为“cv2”的模块错误仅适用于 python3?
- node.js - Npm 安装失败。我克隆了其他 React-router 演示(具有权限功能)。但它在 npm install 上失败了
- docker - Docker 登录到 Docker Desktop for Windows 下 docker stack 中的 nexus 存储库