Sequelize和discord.js - 如何将续集信息转换为简单数组

时间:2018-05-21 10:30:31

标签: sequelize.js discord.js

    const gamem = await gamemodes.findAll({attributes: ['name']});
    const newme = await gamemodes.findAll({attributes: ['name']}, {raw: true });
    console.log(newme)
    const mapi = gamem.map(g => g.name).join('\n') || "No Gamemodes yet!";
    await message.channel.send(`Okay! First, choose the gamemode you'd like to search in. List of gamemodes: **${mapi}**. You have 15 seconds, just post your choice here.`)
const msggg = await message.channel.awaitMessages(msg => msg.content == newme.some(), {time: 15000, max: 1});

所以,newme是这样的:

[ gamemodes {
    dataValues: { name: 'Normal' },
    _previousDataValues: { name: 'Normal' },
    _changed: {},
    _modelOptions:
     { timestamps: true,
       validate: {},
       freezeTableName: false,
       underscored: false,
       underscoredAll: false,
       paranoid: false,
       rejectOnEmpty: false,
       whereCollection: null,
       schema: null,
       schemaDelimiter: '',
       defaultScope: {},
       scopes: [],
       indexes: [],
       name: [Object],
       omitNull: false,
       sequelize: [Object],
       hooks: {},
       uniqueKeys: [Object] },
    _options:
     { isNewRecord: false,
       _schema: null,
       _schemaDelimiter: '',
       raw: true,
       attributes: [Array] },
    __eagerlyLoadedAssociations: [],
    isNewRecord: false },

我想把它变成一个带有awaitMessages的搜索功能,所以当用户从数组输入一个名字时,它会显示其他选项和信息,这是我不会显示的其余代码,因为它不需要。我想要所有的名字,只有gamem(和newme)中的名字输入一个更简单的数组,但我无法弄明白。

当我运行上面的代码时,我收到此错误Invalid value Collection {}

 at Object.escape (D:\BasementMonster\node_modules\sequelize\lib\sql-string.js:66:11)
    at Object.escape (D:\BasementMonster\node_modules\sequelize\lib\dialects\abstract\query-generator.js:934:22)
    at Object.whereItemQuery (D:\BasementMonster\node_modules\sequelize\lib\dialects\abstract\query-generator.js:2141:41)
    at Utils.getComplexKeys.forEach.prop (D:\BasementMonster\node_modules\sequelize\lib\dialects\abstract\query-generator.js:1994:25)
    at Array.forEach (<anonymous>)
    at Object.whereItemsQuery (D:\BasementMonster\node_modules\sequelize\lib\dialects\abstract\query-generator.js:1992:35)
    at Object.getWhereConditions (D:\BasementMonster\node_modules\sequelize\lib\dialects\abstract\query-generator.js:2460:19)
    at Object.selectQuery (D:\BasementMonster\node_modules\sequelize\lib\dialects\abstract\query-generator.js:1140:28)
    at QueryInterface.select (D:\BasementMonster\node_modules\sequelize\lib\query-interface.js:1105:27)
    at Promise.try.then.then.then (D:\BasementMonster\node_modules\sequelize\lib\model.js:1604:34)
    at tryCatcher (D:\BasementMonster\node_modules\bluebird\js\release\util.js:16:23)
    at Promise._settlePromiseFromHandler (D:\BasementMonster\node_modules\bluebird\js\release\promise.js:512:31)
    at Promise._settlePromise (D:\BasementMonster\node_modules\bluebird\js\release\promise.js:569:18)
    at Promise._settlePromise0 (D:\BasementMonster\node_modules\bluebird\js\release\promise.js:614:10)
    at Promise._settlePromises (D:\BasementMonster\node_modules\bluebird\js\release\promise.js:693:18)
    at Async._drainQueue (D:\BasementMonster\node_modules\bluebird\js\release\async.js:133:16)
    at Async._drainQueues (D:\BasementMonster\node_modules\bluebird\js\release\async.js:143:10)
    at Immediate.Async.drainQueues (D:\BasementMonster\node_modules\bluebird\js\release\async.js:17:14)
    at runCallback (timers.js:794:20)
    at tryOnImmediate (timers.js:752:5)
    at processImmediate [as _immediateCallback] (timers.js:729:5)
(node:11156) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:11156) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

我很抱歉这篇文章很长,但我已经尝试了几天了!谢谢!

1 个答案:

答案 0 :(得分:1)

您需要使用.toJSON()

将实例更改为json对象
const newme = await gamemodes.findAll({attributes: ['name']}, {raw: true })
const array = []
newme.map((data) => {
  array.push(data.toJSON())
})

现在您可以根据您修改array