简体   繁体   中英

Sequelize: Query Returns String Array as String, Not as Array of Strings?

I have a Postgres table with a field in it named specialties . The field is defined as:

const myModel = db.define('my_model', {
    id: {type: Sequelize.INTEGER, primaryKey: true, autoIncrement: true},
    createdAt: {type: Sequelize.DATE},
    updatedAt: {type: Sequelize.DATE},
    jobName: {type: Sequelize.STRING},
    specialties: {type: Sequelize.ARRAY(Sequelize.STRING)}
});

I have populated the contents of the specialties field by importing a .csv file with strings in the following format:

["mySpeciality_1", "mySpeciality_2", "mySpeciality_3"]

I'm querying the table like so:

return Promise.resolve()
    .then(() => {
        let specialties = connectors.myModel.findAll({
            where: {'jobName': jobName}
        }).then((specialties) => specialties.map((item) => item.dataValues));
        return specialties;
    })
    .then(([specialties, metaData]) => {
        [.....]

specialties comes back as an object with a field named specialties that contains a string that looks like:

["mySpeciality_1", "mySpeciality_2", "mySpeciality_3"]

Did I import the string array field specialties with strings in the wrong format? Alternatively, do I need to write the sequelize query differently?

Found it.

First of all, I was defining the field as character varying[]. That doesn't seem to work. It seems to want to be text[]. Here's what it looks like in the Valentina postgres client:

在此处输入图片说明

I imported the fields from .csv in this format:

 {mySpeciality_1, mySpeciality_2, mySpeciality_3}

Here's the working sequelize query:

return Promise.resolve()
    .then(() => {
        let theJob = connectors.myModel.findAll({
            where: {'jobName': jobName}
        }).then((theJob) => theJob.map((item) => item.dataValues));
        return theJob;
    })
    .then(([theJob, metaData]) => {
        let specialties = theJob.specialties;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM