Sequelize findOrCreate falling back to catch if data is found

若如初见. 提交于 2019-12-11 10:46:09

问题


FindOrCreate is suppose to either find or create based on the arguments you give it.

So I'm wondering why it's falling back to the catch promise when data (a user) is found, when it should just return the data that was found to the .spread function instead of trying to insert the data that already exists?!.

As you can see below if there is a user found or created it's going to run the same function regardless. All that function is doing is creating an auth token for the particular user. But for some reason it's going straight into the catch promise!?

User.findOrCreate({
    where: {
        userId: details.userId,
    },  
    defaults: {
        name: details.name,
        email: details.email,
    },
    attributes: ['userId', 'name', 'email', 'profilePic'],
    }).spread(function (new_user, created) {
        console.log("\n\nNew user was created T or F: ", created);

       // Create the token and then pass it back to our callback along with the user details
        user.createTokenForUser(new_user.userId, function(token) {
            cb(token, new_user);
        });
}).catch(function (err) {
    // For some reason I'm running...?!??!?!?!?!
    console.log("\n\n", err);
}); 

回答1:


Did you try removing the trailing comma in the where statement? Its just hitting an error and that looks like a culprit :)

where: {
        userId: details.userId,
    }

to

where: {
        userId: details.userId
    }


来源:https://stackoverflow.com/questions/36212942/sequelize-findorcreate-falling-back-to-catch-if-data-is-found

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!