Hello I am trying to connect Node.js with Facebook. I am following this blog entry. Can someone help me?
I am gettin this error:
ValidationError: Validation failed at model.Document.invalidate (/Users/me/node_modules/mongoose/lib/document.js:1009:32) at /Users/me/node_modules/mongoose/lib/document.js:958:16 at validate (/Users/me/node_modules/mongoose/lib/schematype.js:610:7) at /Users/me/node_modules/mongoose/lib/schematype.js:627:9 at Array.forEach (native) at SchemaString.SchemaType.doValidate (/Users/me/node_modules/mongoose/lib/schematype.js:614:19) at /Users/me/node_modules/mongoose/lib/document.js:956:9 at doNTCallback0 (node.js:419:9) at process._tickCallback (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"
}
}
Now I changed the required fields and I am not getting the error.
Validation failed
it means you're trying to save something that doesn't match your schema. The only place you are saving anything (in this code) isprofile.id
in the Facebook strategy. It's probably not a string. – chrisbajorinprofile.id
– chrisbajorin