最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Sequelize findOrCreate(...).spread is not a function - Stack Overflow

programmeradmin1浏览0评论

I'm using the sequelize 6. When I'm runing findOrCreate().spread it says "findOrCreate(...).spread is not a function". Here is my code:

const response = await Response.findOrCreate({
    where: {
        participantId,
        questionId,
    },
    defaults: responseNewDetail,
})
    return res.status(200).send({ status: 0, data: response })

This is working fine, but it does not seperate the created status and the model value. When I'm trying to use spread:

Response.findOrCreate({
    where: {
        participantId,
        questionId,
    },
    defaults: responseNewDetail,
}).spread(function(response,created){
    return res.status(200).send({ status: 0, data: response })
})

It says "Response.findOrCreate(...).spread is not a function". This is the model file(response.js):

const { Sequelize } = require("sequelize")

module.exports = (sequelize, DataTypes) =>
    sequelize.define(
        "Response",
        {
            responseId: {
                type: DataTypes.INTEGER,
                primaryKey: true,
                allowNull: false,
                field: "Response_ID",
                autoIncrement: true,
            },
            panyId: {
                type: DataTypes.INTEGER,
                allowNull: false,
                field: "Company_ID",
            },
...
    )

Response model:

const ResponseModel = require("../models/response")
const Response = ResponseModel(sequelize, DataTypes)

Does anyone know what's wrong?

I'm using the sequelize 6. When I'm runing findOrCreate().spread it says "findOrCreate(...).spread is not a function". Here is my code:

const response = await Response.findOrCreate({
    where: {
        participantId,
        questionId,
    },
    defaults: responseNewDetail,
})
    return res.status(200).send({ status: 0, data: response })

This is working fine, but it does not seperate the created status and the model value. When I'm trying to use spread:

Response.findOrCreate({
    where: {
        participantId,
        questionId,
    },
    defaults: responseNewDetail,
}).spread(function(response,created){
    return res.status(200).send({ status: 0, data: response })
})

It says "Response.findOrCreate(...).spread is not a function". This is the model file(response.js):

const { Sequelize } = require("sequelize")

module.exports = (sequelize, DataTypes) =>
    sequelize.define(
        "Response",
        {
            responseId: {
                type: DataTypes.INTEGER,
                primaryKey: true,
                allowNull: false,
                field: "Response_ID",
                autoIncrement: true,
            },
            panyId: {
                type: DataTypes.INTEGER,
                allowNull: false,
                field: "Company_ID",
            },
...
    )

Response model:

const ResponseModel = require("../models/response")
const Response = ResponseModel(sequelize, DataTypes)

Does anyone know what's wrong?

Share Improve this question asked Aug 12, 2020 at 2:15 MoonMoon 1493 silver badges15 bronze badges 2
  • In your second example, the one with .spread(), are you still using await Response.findOrCreate? – Nick Parsons Commented Aug 12, 2020 at 2:25
  • I'm expecting to use await as the first one if I can.But the problem here is the .spread() is not a function, I want to return the model only instead of include the created status. I'm using the same as stackoverflow./questions/53042399/…, but not working. – Moon Commented Aug 12, 2020 at 2:29
Add a ment  | 

2 Answers 2

Reset to default 4

Since you're using await, you can change:

const response = await Response.findOrCreate({
    where: {
        participantId,
        questionId,
    },
    defaults: responseNewDetail,
})
    return res.status(200).send({ status: 0, data: response })

to

const [ response, created ] = await Response.findOrCreate({
    where: {
        participantId,
        questionId,
    },
    defaults: responseNewDetail,
})
    return res.status(200).send({ status: 0, data: response })

and all will be well.

This doesn't address the spread not a function thing though. For that, I only noticed this when I upgraded sequelize from an older version (I've been using sequelize since version 1). Your example of spread really should be working but for whatever reason it does not anymore (it doesn't for me either). Typically the only time I'm using spread instead of just awaiting it is when I want to defer execution of something. Right or wrong, I've begun to tackle that by just wrapping the await/async version in:

setImmediate(async () => {
    // async things here
});

Hope I helped, sorry if I didn't.

eleborating Brian's Answer :-

findOrCreate returns two values first model instance and second created status, so you can use

const [model,created] = ModelName.findOrCreate();

(Model Response in your case)

to get created status.

发布评论

评论列表(0)

  1. 暂无评论