0

I'm trying to create an instance with associations.

I am passing instances of multiple creates to pass down its values.

const newTemplate = await productTemplate.create({
        friendlyName,
        isActive: true,
        productCode,
        inscoCode,
    }).then((productTemplate) => {

        // Creating Form Input instance
        formInput.create({
            inputName
        }).then((formInput) => {

            // Creating Form Input Values Instance
            formInputValue.create({
                formInputID: formInput.id,
                inputValue: {
                    [Op.in]: inputValues
                }
            }).then((formInputValue) => {

                // Creating Product Template Instance
                productTemplateInput.create({
                    productTemplateID: productTemplate.id,
                    formInputID: formInput.id,
                    isRequired: true
                }).then((productTemplateInput) => {

                    // Creating Product Template Input Instance
                    productTemplateInputValue.create({
                        productTemplateInputID: productTemplateInput.id,
                        formInputValueID: formInputValue.id,
                        isDefault: true
                    })
                })
            })
        })
    });

Here is my formInputValue model.

module.exports = function(sequelize, DataTypes) {
    return sequelize.define('formInputValue', {
        id: {
            type: DataTypes.INTEGER,
            allowNull: false,
            primaryKey: true,
            autoIncrement: true,
            field: 'id'
        },
        formInputID: {
            type: DataTypes.INTEGER,
            allowNull: false,
            field: 'formInputID'
        },
        inputValue: {
            type: DataTypes.ARRAY(DataTypes.STRING),
            allowNull: false,
            field: 'inputValue'
        }
    }, {
        tableName: 'formInputValue',
        timestamps: false
    });
};

I would like to create this instance but stops running when I try to add an array of values to inputValues. Any soutions?

Darron
  • 323
  • 4
  • 14

1 Answers1

0

Why do you have a query operator [Op.in] in your create?

formInputValue.create({
    formInputID: formInput.id,
    inputValue: {
        [Op.in]: inputValues
    }
})

If you remove that, and your inputValues is an array of strings (as defined by your model) it should create them. See below with that operator removed.

formInputValue.create({
    formInputID: formInput.id,
    inputValue: inputValues
})
Rastalamm
  • 1,712
  • 3
  • 23
  • 32
  • I figured it out. The reason we are using [Op.in] is because we are receiving an array. We have not figured out how to receive an array another way. – Darron Aug 05 '19 at 15:37