javascript - Why spread() method doesn't work in Sequelize? - Stack Overflow

I'm using a Sequelize for my node.js app. I use findOrCreate() method to create a new user if not

I'm using a Sequelize for my node.js app. I use findOrCreate() method to create a new user if not exist. Accordingly to docs findOrCreate returns an array containing the object that was found or created and a boolean that will be true if a new object was created and false if not.

The sequelize remend to use spread() method which divides the array into its 2 parts and passes them as arguments to the callback function. First part is a user object and second is boolean if new row was added.

I work in async/await style. My code is:

app.post('/signup', async (req, res) => {
        try {
            let insertedUser = await User.findOrCreate({
                where: { email: req.body.userEmail },
                defaults: {
                    pass: req.body.userPass
                }
            })
            insertedUser.spread((user, created) => {
                if(created) {
                    res.json(user.email)
                }
            })
        } catch (err) {
            console.log(`ERROR! => ${err.name}: ${err.message}`)
            res.status(500).send(err.message)
        }
    }
})

After post request I get an error:

ERROR! => TypeError: insertedUser.spread is not a function

Why is that, the doc says it must be a function?

I'm using a Sequelize for my node.js app. I use findOrCreate() method to create a new user if not exist. Accordingly to docs findOrCreate returns an array containing the object that was found or created and a boolean that will be true if a new object was created and false if not.

The sequelize remend to use spread() method which divides the array into its 2 parts and passes them as arguments to the callback function. First part is a user object and second is boolean if new row was added.

I work in async/await style. My code is:

app.post('/signup', async (req, res) => {
        try {
            let insertedUser = await User.findOrCreate({
                where: { email: req.body.userEmail },
                defaults: {
                    pass: req.body.userPass
                }
            })
            insertedUser.spread((user, created) => {
                if(created) {
                    res.json(user.email)
                }
            })
        } catch (err) {
            console.log(`ERROR! => ${err.name}: ${err.message}`)
            res.status(500).send(err.message)
        }
    }
})

After post request I get an error:

ERROR! => TypeError: insertedUser.spread is not a function

Why is that, the doc says it must be a function?

Share Improve this question asked Oct 29, 2018 at 9:28 NastroNastro 1,7698 gold badges24 silver badges42 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

spread method does not exist on resolved value from findOrCreate. You have to either chain spread with then of promise returned by findOrCreate. Or you need to chain with findOrCreate directly and use await.

Here is the updated code with await:

app.post('/signup', async (req, res) => {
        try {
            let created = await User.findOrCreate({
                where: { email: req.body.userEmail },
                defaults: {
                    pass: req.body.userPass
                }
            }).spread((user, created) => {
                return created;
            })
            if(created) {
                res.json(user.email)
            }
        } catch (err) {
            console.log(`ERROR! => ${err.name}: ${err.message}`)
            res.status(500).send(err.message)
        }
    }
})

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744120549a4559379.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信