need assistance with firestore in javascript - javascript

I want to get the user id on my newly created user so I could name my document to that uid, but it says null.
here's my code
addDriver(){
var $this = this
secondaryApp.auth().createUserWithEmailAndPassword($this.driver.email, $this.driver.password).then(function(user){
$this.$db.collection("driver").doc(user.uid)
.set({
fname: $this.driver.fname,
lname: $this.driver.lname,
contact_number: $this.driver.cnum,
license_number: $this.driver.license,
email: $this.driver.email,
password: $this.driver.password,
})
secondaryApp.auth().signOut();
$this.formHeaderStatus = $this.$db.auth().currentUser
})
},
Also, am I doing this right? coz I'm logged in as an admin? and I want to create a new user without logging myself out.

I am not entirely sure but I believe the problem lies when using the promise returned by createUserWithEmailAndPassword.
From what I have gathered it should be like this :
addDriver(){
var $this = this
secondaryApp.auth().createUserWithEmailAndPassword($this.driver.email, $this.driver.password).then(function(**data**){
$this.$db.collection("driver").doc(**data.user.uid**)
.set({
fname: $this.driver.fname,
lname: $this.driver.lname,
contact_number: $this.driver.cnum,
license_number: $this.driver.license,
email: $this.driver.email,
password: $this.driver.password,
})
secondaryApp.auth().signOut();
$this.formHeaderStatus = $this.$db.auth().currentUser
})
},
Hope it helps, if not, let me know.
But as Franck said, if you want to sign a new user up while staying logged-in, you should use the Admin-SDK in your back end or Cloud Functions

Related

Password confirmation and password are equal but validation is still triggering JOI in React component

I creating a register form and the problems accurs while try validating password confirmation. I am using the last version of JOI-Browser.
I tried the code below and the validation error was triggered even though password and password confirmation have the same values.
password: Joi.string()
.min(5)
.required(),
passwordConfirmation: Joi.ref("password")
Here is my state object:
password: "12345"
passwordConfirmation: "12345"
username: ""
errors: {…}
passwordConfirmation: "\"passwordConfirmation\" must be one of [ref:password]"
I passed several hours trying several approaches and reading the documentation, but still no luck, the validation is still triggering,
I have other validations in this form and they work fine.
I don't think Joi.ref should be used that way.
I usually tend to do this way:
const passwordConfirmation = Joi.string()
.required()
.valid(Joi.ref('password'))
.options({
language: {
any: {
allowOnly: '!!Passwords do not match',
}
}
})
If you refer to the docs, you will see:
Note that references can only be used where explicitly supported such as in valid() or invalid() rules. If upwards (parents) references are needed, use object.assert().
If anyone has encountered a similar problem, this is the solution I have used:
validateProperty = (input) => {
let obj = { [input.name]: input.value };
let schema = { [input.name]: this.schema[input.name] };
if (input.name.endsWith("_confirm")) {
const dependentInput = input.name.substring(
0,
input.name.indexOf("_confirm")
);
obj[dependentInput] = this.state.data[dependentInput];
schema[dependentInput] = this.schema[dependentInput];
}
const { error } = Joi.validate(obj, schema);
return error ? error.details[0].message : null;
};
In my case, I have looked for _confirm because I have the field names as password and password_confirm. You need to make changes here as per your requirements.
Main logic, you just need to add value and schema of password when you are validating password_confirm
I find out what was happening. My code above was right, the problem was in my validate function.
Gabriele's Petrioli comment help me out. this is the function that cause me problems:
validateProperty = ({ name: propertyName, value }) => {
const obj = { [propertyName]: value };
const schema = { [propertyName]: this.schema[propertyName] };
const { error } = Joi.validate(obj, schema);
return error ? error.details[0].message : null;};
Has you can see i tried validate each property individually, so i can make the form more dynamic.
This trigger the validation error because when i try to validate confirmPassword there was no value in password because i passed only the value the correspond to confirmaPassword, it also needed the password value to make the comparison.
Rookie mistake

JS library to check if extra data field returned in http response payload?

I’m currently working on building end-to-end testing for an API another team is working on, and I was wondering if anyone perhaps knows about a JS library that I could use to test whether an extra field is returned in HTTP response body? The purpose of this functionality would be to keep the QA team informed when the dev team makes changes to the api via the tests, instead of the developers manually having to let us know they’ve created updates. I know this can be implemented manually but if the wheel already exists, I’d prefer to avoid recreating it lol.
Example scenario:
API call: GET user
- returns : user name, user ID and user birthday.
With proposed functionality, if the dev team made updates to the Get user call, and it returns the following
- return : user name, user ID, user birthday AND user address.
A test would fail to let me know that an extra field that wasn't expected (user address) was returned.
Schema validation seems to be what you are looking for. Besides the library mentioned in another answer, you may also want check a similar one: joi
const Joi = require('joi');
const schema = Joi.object().keys({
userName: Joi.string().alphanum().required(),
userId: Joi.number().required(),
userBirthDay: Joi.number().required(),
})
const result = Joi.validate({
userName: 'johndoe',
userId: 1234567,
userBirthDay: 1970,
userAddress: 'John Doe St.'
}, schema);
if (result.error) {
console.log(result.error.details);
}
In the spec you can make assertion on existence of error key in result object using the assertion library of your choice.
The example above assumes that you are using nodejs as an environment to run tests, but browser version of joi also exists: joi-browser
You need schema validation, there are libraries out there like ajv.
var ajv = new Ajv({ allErrors: true }); // options can be passed, e.g. {allErrors: true}
// API call: GET user - returns : user name, user ID and user birthday.
// With proposed functionality, if the dev team made updates to the Get user call, and it returns the following - return : user name, user ID, user birthday AND user address.
var schema = {
type: "object",
properties: {
userName: {
type: "string",
},
userId: {
type: "string",
},
userBirthdate: {
type: "string",
},
},
required: ["userName", "userId", "userBirthdate"],
additionalProperties: false,
};
var validate = ajv.compile(schema);
var validUser = {
userName: "John",
userId: "john",
userBirthdate: "01012000",
};
var invalidUser = {
userName: "John",
userId: "john",
userBirthdate: "01012000",
userAddress: "World",
};
var valid = validate(validUser);
console.log(`Valid user is valid: ${valid}`);
valid = validate(invalidUser);
console.log(`Invalid user is valid: ${valid}`);
console.log('Validate errors:', validate.errors);
<script src="https://cdnjs.cloudflare.com/ajax/libs/ajv/6.6.2/ajv.min.js"></script>

Insert data in collection at Meteor's startup

I would like to insert data at Meteor's startup. (And after from a JSON file)
At startup, I create a new account and I would like to insert data and link it to this account once this one created.
This is the code that creates the new account at startup:
if(!Meteor.users.findOne({emails: { $elemMatch: { address: "test#test.com"}}})){
var id = Accounts.createUser({ email: "test#test.com", password: "1234", profile: { name: 'Test' } });
Meteor.users.update({_id: id }, { $set: { admin: false }});
}
And after that, I need to insert data and link it to this account with its ID. (In different collections).
So I tried to do something like that, but obviously It didn't work:
UserData = new Mongo.Collection('user_data');
if(!Meteor.users.findOne({emails: { $elemMatch: { address: "test#test.com"}}})){
var id = Accounts.createUser({ email: "test#test.com", password: "1234", profile: { name: 'Test' } });
Meteor.users.update({_id: id }, { $set: { admin: false }});
UserData.insert({
createdBy: id,
firstname: "test",
/* ... */
});
}
EDIT
Sorry for not have been clear.
The real issue is the :
UserData = new Mongo.Collection('user_data');
declaration is in another file, so I can't do like above.
As it's not in the same file, I tried to get the userId that got "test#test.com" as the email (the account's email created at startup). And once I got it, I want to use it in "createdBy: ID_HERE".
Ok, you'll want to check out Structuring your application. You'll have to make the file with the definition load earlier, or the one with the fixture later.
Normally you have your collections inside lib/ and your fixtures inside server/fixtures.js.
So if you put your insert code into server/fixtures.js it'll work.

generate custom key with $add angularfire

How can I generate a custom key in array inside angularfire when I add a new record with the $add function. Just look at when the is the comment below in the source code. This is my code below but still get the random key generated by firebase.
register : function(user){
return simpleLogin.$createUser({
email: user.email,
password: user.password
}).then(function(regUser){
//console.dir(regUser);
var ref = new Firebase(FIREBASE_URL + 'users');
var firebaseUsers = $firebaseArray(ref);
var userInfo = {
date: Firebase.ServerValue.TIMESTAMP,
regUser: regUser.uid,
firstname: user.firstname,
lastname: user.lastname,
email: user.email
}
//this is when i want to generate the key
firebaseUsers.$add(userInfo).then(function(ref) {
});
});
},//register
Calling $add will simply always result in a so-called push id. If you don't want to use those push ids to identify the objects, the solution is to not call $add/push.
Instead you can just access the child that you want to create directly:
var ref = new Firebase(FIREBASE_URL + 'users');
var userInfo = {
date: Firebase.ServerValue.TIMESTAMP,
regUser: regUser.uid,
firstname: user.firstname,
lastname: user.lastname,
email: user.email
}
ref.child(regUser.uid).set(userInfo);
Two things to note here:
the snippet creates the child node under regUser.uid. If you need another key, you can substitute that.
this snippet doesn't use AngularFire, but instead uses the Firebase JavaScript SDK directly. Since AngularFire is built on the JavaScript SDK, they interoperate perfectly. So if you need an operation that isn't immediately obvious in AngularFire, it might be worth it to check whether you can accomplish it (more easily) with the regular Firebase JavaScript SDK.
with the help of mester Frank van Puffelen above I modify the code :
register : function(user){
return simpleLogin.$createUser({
email: user.email,
password: user.password
}).then(function(regUser){
//console.dir(regUser);
var ref = new Firebase(FIREBASE_URL + 'users');
var firebaseUsers = $firebaseArray(ref);
var userInfo = {
date: Firebase.ServerValue.TIMESTAMP,
regUser: regUser.uid,
firstname: user.firstname,
lastname: user.lastname,
email: user.email
}
//the added portion of code
ref.child(regUser.uid).set(userInfo);
firebaseUsers.$add(userInfo).then(function(ref) {
});
});
},//register
and i got the result i want , in angularfire API there is no functions ( 'child()' and set() ) but as mentionned in the post of mester Frank van Puffelen angularfire API is built up on firebase API .
and this is my capture image from my database firebase.

Mongoose JS findOne always returns null

I've been fighting with trying to get Mongoose to return data from my local MongoDB instance; I can run the same command in the MongoDB shell and I get results back. I have found a post on stackoverflow that talks about the exact problem I'm having here; I've followed the answers on this post but I still can't seem to get it working. I created a simple project to try and get something simple working and here's the code.
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var userSchema = new Schema({
userId: Number,
email: String,
password: String,
firstName: String,
lastName: String,
addresses: [
{
addressTypeId: Number,
address: String,
address2: String,
city: String,
state: String,
zipCode: String
}
],
website: String,
isEmailConfirmed: { type: Boolean, default: false },
isActive: { type: Boolean, default: true },
isLocked: { type: Boolean, default: false },
roles: [{ roleName: String }],
claims: [{ claimName: String, claimValue: String }]
});
var db = mongoose.connect('mongodb://127.0.0.1:27017/personalweb');
var userModel = mongoose.model('user', userSchema);
userModel.findOne({ email: 'test#test.com' }, function (error, user) {
console.log("Error: " + error);
console.log("User: " + user);
});
And here is the response of the 2 console.log statements:
Error: null
User: null
When the connect method is called I see the connection being made to my Mongo instance but when the findOne command is issued nothing appears to happen. If I run the same command through the MongoDB shell I get the user record returned to me. Is there anything I'm doing wrong?
Thanks in advance.
Mongoose pluralizes the name of the model as it considers this good practice for a "collection" of things to be a pluralized name. This means that what you are currently looking for in code it a collection called "users" and not "user" as you might expect.
You can override this default behavior by specifying the specific name for the collection you want in the model definition:
var userModel = mongoose.model('user', userSchema, 'user');
The third argument there is the collection name to be used rather than what will be determined based on the model name.
I know 7 years pass, however I'm starting to develop in node JS with MongoDB using mongoose, so searching for solution to the same problem, result = null.
After read this post and all the answers, I release that I totally forget to include the DB name in the string of the connection, 'mongodb://localhost:27017/DB name' so that solved my case. I guessed this can be help other clueless like me! :)

Categories