【问题标题】:Passport.js Spotify Strategy - User.Save() is not a functionPassport.js Spotify 策略 - User.Save() 不是函数
【发布时间】:2020-04-05 17:00:23
【问题描述】:

我正在尝试在我的数据库中保存访问权限以及刷新令牌,然后为该用户进行 API 调用。对于 Passport-js-spotify、JMPerez Passport Spotify 示例和 Spotify API 文档,我已尽我所能地遵循在线示例,但我仍然收到一条错误消息,提示 User.save() 不是函数。如果我只是 findOne,我就可以从数据库中检索用户信息。此外,我感觉我的回调设置也不正确。有点转身。我将不胜感激任何解决问题的帮助。我在下面包含了我的代码。如果您需要任何其他信息,请告诉我。

const express = require("express");
const router = express.Router();
const SpotifyStrategy = require("passport-spotify").Strategy;
const passport = require("passport");
const SpotifyUser = require("../Models/UserSpotify");

var spotifyID;

passport.use(
    new SpotifyStrategy({
        clientID: "84f1c0xxxx29b95xxx0b0fdxxx42f5ce",
        clientSecret: "0b0xxx5944ad8bxx07ab4xxxx",
        callbackURL: "http://localhost:3001/spotify/callback",
    }, function (accessToken, refreshToken, expires_in, profile, done) {

        spotifyID = profile.id
        displayName = profile.displayName
        email = profile._json.email
        profileURL = profile.profileUrl
        accessToken = accessToken
        refreshToken = refreshToken
        country = profile.country
        accountType = profile.product

        console.log(spotifyID)

        process.nextTick(function () {
            SpotifyUser.findOne({
                'spotifyID': spotifyID
            }).then((currentUser) => {
                if (!currentUser) {
                    const User = new SpotifyUser == ({
                        spotifyID: spotifyID,
                        displayName: displayName,
                        email: email,
                        profileURL: profileURL,
                        accessToken: accessToken,
                        refreshToken: refreshToken,
                        country: country,
                        accountType: accountType
                    })
                    User.save(function (err) {
                        if (err) console.log(err);
                        return done(err, user);
                    })
                } else {
                    console.log(currentUser);
                }
            })

        })


router.get(
    "/login",
    passport.authenticate(
        "spotify", {
            scope: ["user-read-email", "user-read-private"],
            showDialog: true,
        }
    ),
    function (req, res) {
        console.log(res.accessToken)
        console.log(res.refreshToken)
    }
);

router.get(
    "/callback",
    passport.authenticate("spotify", {
        failureRedirect: "/login"
    }),
    function (req, res) {
        // Successful authentication, redirect home.
        res.redirect("/success");

    }
);

router.get("/success", passport.authenticate('spotify'), (req, res) => {
    res.send("success")

});

【问题讨论】:

    标签: node.js express mongoose passport.js spotify


    【解决方案1】:

    我在代码中看到的一件显而易见的事情是

     const User = new SpotifyUser == ({
                            spotifyID: spotifyID,
                            displayName: displayName,
                            email: email,
                            profileURL: profileURL,
                            accessToken: accessToken,
                            refreshToken: refreshToken,
                            country: country,
                            accountType: accountType
                        })
    

    删除==

    const User = new SpotifyUser({
                            spotifyID: spotifyID,
                            displayName: displayName,
                            email: email,
                            profileURL: profileURL,
                            accessToken: accessToken,
                            refreshToken: refreshToken,
                            country: country,
                            accountType: accountType
                        })
    

    【讨论】:

    • 嗨,ssk,当我删除 == 并添加 = 时,我收到此错误--> const User = new SpotifyUser = ({ SyntaxError: Invalid left-hand side in assignment
    • 也不需要=,请参阅答案中粘贴的代码。
    • 嗨,ssk,我想这就是我没有走开并以新鲜的眼光回来的结果。我真的很感谢你指出这一点。我能够保存到数据库中。非常感谢您的帮助。如果我在这里有更好的声誉,我的支持会更重要。
    • 不错。如果问题解决了,您可以将其标记为已回答。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-11
    • 2019-01-11
    • 2015-05-22
    • 2012-10-04
    • 1970-01-01
    • 1970-01-01
    • 2018-11-10
    相关资源
    最近更新 更多