【发布时间】:2023-04-06 16:36:01
【问题描述】:
这是我在这里的第一篇文章,所以请温柔。 我在 Node 中使用带有 PostgresDB 的 Sequelize 并尝试使用 findOrCreate 通过电子邮件搜索联系人。电子邮件条目采用 JSON 格式(参见下面的结构)...
我想通过电子邮件查找联系人,并为每个联系人条目遍历 JSON 数组中的每个电子邮件地址。
我在 POST 有效负载中传递了一个电子邮件地址和一个名称。
如果我无法通过电子邮件找到联系人,我想创建一个新联系人(这部分很简单)。
router.post('/gmail/fetchcontact', function (req, res, next){
Contacts.findOrCreate({
where: {
email: // this is where I am stuck. How do I access this array and loop over?
},
defaults: {
name: req.body.name //
}
})
.then(response => {
if (response[1] === true) {
console.log('RESPONSE!!!', response[1])
}
})
// 这是我希望迭代的 JSON 的结构
[{
address: 'your.name@gmail.com',
primary: true,
rel: 'http://schemas.google.com/g/2005#home'
},{
address: 'work@work.com',
primary: false,
labels: ['work'],
},{
address: 'play@play.com',
primary: false,
labels: ['play'],
}]
有什么想法吗?任何帮助将不胜感激。谢谢!
【问题讨论】:
标签: javascript json node.js postgresql sequelize.js