如果找到数据则回退到捕获

如果找到数据则回退到捕获

本文介绍了Sequelize findOrCreate 如果找到数据则回退到捕获的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

FindOrCreate 假设根据您提供的参数findcreate.

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

所以我想知道为什么在找到 data (a user) 时它应该只返回 datacatch 承诺/code> 被发现到 .spread 函数,而不是尝试 insert 已经存在的数据?!

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?!.

正如您在下面看到的,如果找到或创建了一个用户,它无论如何都会运行相同的功能.该函数所做的只是为特定用户创建一个 auth token.但出于某种原因,它直接进入了 catch 承诺!?

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);
});

推荐答案

您是否尝试删除 where 语句中的尾随逗号?它只是遇到了一个错误,看起来像是罪魁祸首:)

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,
    }

where: {
        userId: details.userId
    }

这篇关于Sequelize findOrCreate 如果找到数据则回退到捕获的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 07:19