Sequelize 普通对象返回空 - 花括号行为不端
Posted
技术标签:
【中文标题】Sequelize 普通对象返回空 - 花括号行为不端【英文标题】:Sequelize plain object returns empty - curly braces misbehaving 【发布时间】:2021-10-24 02:50:30 【问题描述】:场景: 当我试图从 sequelize 查询中获取一个普通对象时,我得到了一组 3 个空对象。
解决方案: 经过几个小时的尝试,我注意到当我用花括号包围 message.get 方法时,它会停止正常工作。
有人能解释一下为什么会这样吗?通过用花括号包围 message.get(plain:true) 方法,它给出了一组空对象(在这种特殊情况下为 3 个空对象)。
原代码:
const router = require('express').Router();
const Message = require('../models/');
router.get('/', async (req, res) =>
try
const dbMessagesData = await Message.findAll();
// HERE *****************************************
const messages = dbMessagesData.map(message =>
message.get(plain: true);
);
// MESSAGES CONTENT
messages.forEach(( element , index, array)=>
console.log("---> element :" + JSON.stringify (element) );
);
res.render('homepage', messages);
catch
(e)
res.status(400).send(e.message);
);
module.exports = router;
终端:
"C:\Program Files\nodejs\node.exe" C:\Users\giann\Dropbox\IdeaProjects\Homework\14-Tech-Blog\server.js
(node:97384) [SEQUELIZE0002] DeprecationWarning: The logging-option should be either a function or false. Default: console.log
(Use `node --trace-deprecation ...` to show where the warning was created)
Server listening on port 3000
Executing (default): SELECT `id`, `title`, `content`, `created_at` AS `createdAt`, `updated_at` AS `updatedAt`, `user_id`, `user_id` AS `UserId` FROM `Message` AS `Message`;
---> element :undefined
---> element :undefined
---> element :undefined
现在,我把花括号块去掉了:
const router = require('express').Router();
const Message = require('../models/');
router.get('/', async (req, res) =>
try
const dbMessagesData = await Message.findAll();
//HERE - curly braces removed ****************
const messages = dbMessagesData.map(message =>
message.get(plain: true)
);
// MESSAGES CONTENT
messages.forEach(( element , index, array)=>
console.log("---> element :" + JSON.stringify (element) );
);
res.render('homepage', messages);
catch
(e)
res.status(400).send(e.message);
);
module.exports = router;
终端:
"C:\Program Files\nodejs\node.exe" C:\Users\giann\Dropbox\IdeaProjects\Homework\14-Tech-Blog\server.js
(node:188548) [SEQUELIZE0002] DeprecationWarning: The logging-option should be either a function or false. Default: console.log
(Use `node --trace-deprecation ...` to show where the warning was created)
Server listening on port 3000
Executing (default): SELECT `id`, `title`, `content`, `created_at` AS `createdAt`, `updated_at` AS `updatedAt`, `user_id`, `user_id` AS `UserId` FROM `Message` AS `Message`;
---> element :"id":1,"title":"Title 1","content":"Hey, this is the first message","createdAt":"2021-08-23T17:29:56.000Z","updatedAt":"2021-08-23T17:29:56.000Z","user_id":1,"UserId":1
---> element :"id":2,"title":"Title 2","content":"Hey, this is the second message","createdAt":"2021-08-23T17:29:56.000Z","updatedAt":"2021-08-23T17:29:56.000Z","user_id":2,"UserId":2
---> element :"id":3,"title":"Title 3","content":"Hey, this is the third message","createdAt":"2021-08-23T17:29:56.000Z","updatedAt":"2021-08-23T17:29:56.000Z","user_id":3,"UserId":3
``````
What am I missing here??
【问题讨论】:
【参考方案1】:它只是语法(记住它)
(x) => x
等价于
(x) =>
return x;
如果您不将退货放在第二个表格中,您将没有退货,这意味着 它将返回未定义。因此你所看到的。
在你的例子中这应该可以工作
const messages = dbMessagesData.map(message =>
return message.get(plain: true);
);
【讨论】:
我的天哪!我很高兴没有人的生活取决于我的代码!谢谢拉斐尔。以上是关于Sequelize 普通对象返回空 - 花括号行为不端的主要内容,如果未能解决你的问题,请参考以下文章
如果没有花括号并在换行后返回,那么这个带有“空”的 C 程序会发生啥?
Sequelize - 在普通对象中包含 getter 返回