0

This is my first post here so please be gentle. I am using Sequelize w/ PostgresDB in Node and trying to search for a contact by email using findOrCreate. The email entry is in JSON format (see structure below)...

I want to find a contact by email and iterate over each email address in the JSON array for each contact entry.

I am passing in an email address and a name in the POST payload.

If I cannot find the contact by email I want to create a new contact (this part is easy).

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])
        }

      })

// THIS IS THE STRUCTURE OF THE JSON I AM LOOKING TO ITERATE OVER

[{
          address: '[email protected]',
          primary: true,
          rel: 'http://schemas.google.com/g/2005#home'
        },{
          address: '[email protected]',
          primary: false,
          labels: ['work'],
        },{
          address: '[email protected]',
          primary: false,
          labels: ['play'],
        }]

Any ideas? Any help would be greatly appreciated. Thanks!

2 Answers 2

1

I found a great solution for this...

First, you must change the datavalue to type: Sequelize.JSONB

Then you can iterate over nested JSON objects and arrays like so:

 Contacts.findOne({
    where: {
      email: {
        $contains: [{
          address: req.body.email
        }]
      },
      UserId: req.user.id
    }
  })

** Please note that in $contains I wrapped the object in an array

Sign up to request clarification or add additional context in comments.

Comments

0

I would create an array of just the emails and then do an $in search.

const emails = [{
      address: '[email protected]',
      primary: true,
      rel: 'http://schemas.google.com/g/2005#home'
    },{
      address: '[email protected]',
      primary: false,
      labels: ['work'],
    },{
      address: '[email protected]',
      primary: false,
      labels: ['play'],
    }].map(email => email.address);

Search:

Contacts.findOrCreate({
    where: {
      email: { $in: emails }
    },
    defaults: {
      name: req.body.name // 
    }
  })
  .then(response => {
    if (response[1] === true) {
      console.log('RESPONSE!!!', response[1])
    }
  });

Documentation on $in is here: http://docs.sequelizejs.com/manual/tutorial/models-usage.html#-findall-search-for-multiple-elements-in-the-database

1 Comment

Thank you much for the reply. Appreciate it., will try out the code and circle back if it works...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.