0

I have a function that records working data. Then I insert this array into the request.

async function setJobs(jobs) {
    const insertFieldsOfJob = [];
    for (const key of jobs) {
        const jobFields = [
            `'${key.job_id}'`,
            `'${key.description}'`,
            `'${key.verified}'`,
            `'${key.amount}'`,`,
            `'${key.title}'`,
            `'${key.ctime}'`,
        ];
        insertFieldsOfJob.push(`(${jobFields})`);
    };
    const jobsIds = (await db.query(Jobs.writeJobs(insertFieldsOfJob))).rows;
    return jobsIds;
};

Here I am writing to the job details database

class Job {
    static writeJobs(jobsDetails) {
        const sql = `INSERT INTO JOBS (JOB_ID, DESCRIPTION, VERIFIED, AMOUNT, TITLE, CTIME) 
                VALUES 
                    ${jobsDetails} 
                        RETURNING ID`
        return sql;
    }
}

module.exports = Job;

I get data

jobs {
    job_id: 1,
    description: test,
    verified: false,
    amount: 500.00,
    title: test,
    ctime: 1564461408000,
}

And I get errors

error: invalid integer value: "500.00"
error: the value "1564461408000" is out of range for type integer

Why it happens? I refer to the properties of the object as a string.

2
  • Well that mean you have given Int type to your columns when you should be giving Float / Double for amount and timestamp / date-time for ctime depending upon your DB. Commented Jul 30, 2019 at 5:22
  • @prakash-thete I for all specified type varchar 220 Commented Jul 30, 2019 at 5:29

1 Answer 1

1

Try to use String() and remove some parentheses.

async function setJobs(jobs) {
    const insertFieldsOfJob = [];
    for (const key of jobs) {
        const jobFields = [
            `${String(key.job_id)}`,
            `${String(key.description)}`,
            `${String(key.verified)}`,
            `${String(key.amount)}`,
            `${String(key.title)}`,
            `${String(key.ctime)}`,
        ];
        insertFieldsOfJob.push(`${jobFields}`);
    };
    const jobsIds = (await db.query(Jobs.writeJobs(insertFieldsOfJob))).rows;
    return jobsIds;
};
Sign up to request clarification or add additional context in comments.

2 Comments

No error, but the value is not wrapped in single quotes
Why do you nedd single quotes? The values are already strings

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.