【问题标题】:How do I get data from Mongodb如何从 Mongodb 获取数据
【发布时间】:2019-09-29 00:37:46
【问题描述】:

我有这个sn-p:

const slackProfiles = await Promise.all(

checkinsDetails.map(async ({ employeeEmail }) => {
    const employeeData = Employee.findOne({
      workEmail: employeeEmail,
    });

    console.log('employee ', employeeData);

    const slackId = employeeData ? employeeData.slackId : '';

当我在控制台中记录员工数据时,我得到了这种数据:

employee  Query {
   _mongooseOptions: {},
   _transforms: [],
   _hooks: Kareem { _pres: Map {}, _posts: Map {} },
   _executionCount: 0,
   mongooseCollection:
   NativeCollection {
      collection: null,
 opts:
  { bufferCommands: true,
    capped: false,
    '$wasForceClosed': undefined },
 name: 'employees',
 collectionName: 'employees',
 conn:
  NativeConnection {
    base: [Mongoose],
    collections: [Object],
    models: [Object],
    config: [Object],
    replica: false,
    options: null,
    otherDbs: [],
    relatedDbs: {},
    states: [Object],
    _readyState: 0,
    _closeCalled: false,
    _hasOpened: false,
    '$internalEmitter': [EventEmitter],
    _listening: false },
 queue: [],
 buffer: true,
 emitter:
  EventEmitter {
    _events: [Object: null prototype] {},
    _eventsCount: 0,
    _maxListeners: undefined } },
 model:
{ [Function: model]
 hooks: Kareem { _pres: [Map], _posts: [Map] },
 base:
  Mongoose {
    connections: [Array],
    models: [Object],
    modelSchemas: [Object],
    options: [Object],
    _pluralize: [Function: pluralize],
    Schema: [Function],
    model: [Function],
    plugins: [Array] },
 modelName: 'Employee',
 ...

当我在EMployee.findOne({}) 之前附加await 时,控制台中不会记录任何内容。我也试过了

Employee.findOne({
      workEmail: employeeEmail,
    },(err,data)=>console.log("data",data);

但它不输出任何东西。

【问题讨论】:

    标签: node.js mongodb mongoose


    【解决方案1】:

    你应该试试这个,我认为这对你有用。

    Employee.findOne({workEmail: employeeEmail}).then(err, result) {console.log(result)};
    

    【讨论】:

    • 这应该可以,我认为问题是因为调用是从子进程发出的
    • @MeshackMbuvi 你能通过这个解决方案解决你的问题吗
    【解决方案2】:

    console.log 输出的是查询而不是结果。此外,从 Node.js 在 MongoDB 上触发查询的回调格式不需要等待/异步。

    假设Employee 是一个集合对象,以下应该可以工作。在这种情况下避免异步/等待,因为回调函数会处理它。

    Employee.findOne({
          workEmail: employeeEmail,
        },(err,data)=>console.log("data",data);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-28
      • 2013-11-06
      • 1970-01-01
      相关资源
      最近更新 更多