猫鼬返回属性的未定义值

时间:2020-07-30 16:57:24

标签: javascript node.js mongodb mongoose

好吧,我试图通过Mongoose find()从对象中获取属性值,但是由于某些原因,猫鼬返回的是未定义的值。

模式:

const mongoose = require('mongoose');
const uuid = require('uuid');

const Schema = mongoose.Schema({
  dsID: { type: String, unique: true, require: true },
  dsTag: { type: String },

  mcCode: { type: String, default: () => uuid.v4(), unique: true, select: false },
  mcConnected: { type: Boolean, default: false }

}, { versionKey: false });

const Members = mongoose.model("Members", Schema);
module.exports = Members;

代码

// Database connection
mongoose.connect(DATABASE.uri, DATABASE.options);

Members.find({ 'dsID': dsID }, (err, member) => {
  const connected = member.mcConnected;

  console.log(connected)
});

2 个答案:

答案 0 :(得分:0)

这可能是因为您不应将模型命名为“ Schema”。请尝试使用其他名称,因为“ Schema”是保留字

答案 1 :(得分:0)

在架构上使用此代码

const mongoose = require('mongoose');
const uuid = require('uuid');

const memberSchema = new mongoose.Schema({
  dsID: { type: String, unique: true, require: true },
  dsTag: { type: String },

  mcCode: { type: String, default: () => uuid.v4(), unique: true, select: false },
  mcConnected: { type: Boolean, default: false }

}, { versionKey: false });

const Members = mongoose.model("Members", memberSchema);
module.exports = Members;