转换为ObjectId失败

时间:2020-03-03 06:51:28

标签: javascript node.js graphql

我想使用findById将一个字段输入数据库,但是当我运行它时,我会遇到此错误

“无法成功将对象\ Id转换为值\”查询

{\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); */
    });
}

0 个答案:

没有答案
相关问题