您好我正在尝试将Node.js与Facebook连接。我正在关注this博客条目。 有人能帮我吗?
我遇到了这个错误:
ValidationError:在model.Document.invalidate上验证失败 (/Users/me/node_modules/mongoose/lib/document.js:1009:32)at at /Users/me/node_modules/mongoose/lib/document.js:958:16验证 (/Users/me/node_modules/mongoose/lib/schematype.js:610:7)at at /Users/me/node_modules/mongoose/lib/schematype.js:627:9 at SchemaString.SchemaType.doValidate中的Array.forEach(native) (/Users/me/node_modules/mongoose/lib/schematype.js:614:19)at at /Users/me/node_modules/mongoose/lib/document.js:956:9 at 在process._tickCallback上执行doNTCallback0(node.js:419:9) (node.js中:348:13)
var FacebookStrategy = require('passport-facebook').Strategy;
var BearerStrategy = require('passport-http-bearer').Strategy;
var passport = require('passport');
var mongoose = require('mongoose');
var ObjectId = require('mongoose').Types.ObjectId;
// Mongoose connection to MongoDB (ted/ted is readonly)
mongoose.connect('mongodb://localhost/testDB', function(error) {
if (error) {
console.log(error);
}
});
var userSchema = new Schema({
facebookId: String,
access_token: {
type: String
},
profileImage : String,
email : {
type : String,
unique : true
},
password : {
type : String,
},
created_at : Date,
updated_at : Date
});
userSchema.statics.findOrCreate = function(filters, cb) {
User = this;
this.find(filters, function(err, results) {
if(results.length == 0) {
var newUser = new User();
newUser.facebookId = filters.facebookId;
newUser.save(function(err, doc) {
cb(err, doc);
});
} else {
cb(err, results[0]);
}
});
};
//facebook auth setup
options = {
clientID: 'FB_ID',
clientSecret: 'FB_SecretKey',
callbackURL: 'http://localhost:3000/auth/facebook/callback'
};
passport.use(
new FacebookStrategy(
options,
function(accessToken, refreshToken, profile, done) {
User.findOrCreate(
{ facebookId: profile.id },
function (err, result) {
if(result) {
result.access_token = accessToken;
result.save(function(err, doc) {
done(err, doc);
});
} else {
done(err, result);
}
}
);
}
)
);
app.get(
'/auth/facebook',
passport.authenticate('facebook', { session: false, scope: [] })
);
app.get('/auth/facebook/callback',
passport.authenticate('facebook', { session: false, failureRedirect: "/" }),
function(req, res) {
res.redirect("/");
}
);
//token auth setup
passport.use(
new BearerStrategy(
function(token, done) {
User.findOne({ access_token: token },
function(err, user) {
if(err) {
return done(err);
}
if(!user) {
return done(null, false);
}
return done(null, user, { scope: 'all' });
}
);
}
)
);
的package.json
{
"name": "testapp",
"version": "0.0.0",
"description" : "Something"
"engines": {
"node": "4.2.x",
"npm": "3.3.x",
},
"dependencies": {
"body-parser": "~1.12.0",
"cookie-parser": "~1.3.4",
"debug": "~2.1.1",
"express": "~4.12.2",
"fs-extra": "^0.18.4",
"jade": "~1.9.2",
"jsonwebtoken": "5.0.4",
"morgan": "~1.5.1",
"multer": "^1.0.3",
"serve-favicon": "~2.2.0"
}
}
现在我更改了必填字段,但我没有收到错误。
答案 0 :(得分:0)
password : {
type : String, // <-- no
}
这可能是一个原因