首页 > 解决方案 > 方法不返回整个对象

问题描述

当我调用 buildCommand 方法时,它不会返回属性消息,但我发现如果我从 buildCommand 中删除一些属性,它就可以工作。这是我调用的方法

const buildCommand = (commandJSON) => {
        return new Command({
        prefix: commandJSON.prefix,
        command: commandJSON.command,
        aliases: commandJSON.aliases,
        parameters: commandJSON.parameters,
        message: commandJSON.message,
        response: commandJSON.response,
        commandMedium: commandJSON.commandMedium,
        enabled: commandJSON.enabled,
        isDefault: commandJSON.isDefault,
        permission: commandJSON.permission,
        cooldown: commandJSON.cooldown,
      });
    };

这就是我调用该方法的方式

const newCommand = buildCommand(commandJSON);

commandJSON 看起来像这样

{ prefix: '!', command: 'laugh', message: 'hahaha' }

更新 2 这是我的整个命令模型

const mongoose = require('mongoose');

const commandSchema = mongoose.Schema({
  prefix: {
    type: String,
    default: '!',
  },
  command: {
    type: String,
    required: true,
  },
  aliases: {
    type: Array,
  },
  parameters: {
    type: Array,
  },
  message: {
    type: String,
  },
  response: {
    type: String,
    enum: ['chat', 'whisper'],
    default: 'chat',
  },
  commandMedium: {
    type: String,
    enum: ['offline', 'online', 'both'],
    default: 'both',
  },
  enabled: {
    type: Boolean,
    default: true,
  },
  isDefault: {
    type: Boolean,
    default: false,
  },
  permission: {
    type: String,
    enum: ['everyone', 'subscriber', 'vip', 'moderator', 'broadcaster'],
    default: 'everyone',
  },
  cooldown: {
    globalCooldown:{type:Boolean, default:false},
    globalDuration:{type:Number, default:0},
    userDuration:{type:Number,default:0},
  }
});

module.exports = mongoose.model('Commands', commandSchema, 'TwitchUsers');

标签: javascriptnode.jsmongodbasynchronousasync-await

解决方案


Command 只是一个 Mongoose 模型。那里没有任何异步内容,您可以(并且应该)删除这些async/await东西。

你可以简单地做const newCommand = new Command(commandJSON),工作完成。


推荐阅读