Hello guys,
I want to develop dating app by using Nodejs and mongodb to create an api and to the front side React Native.
My idea is when user register, he will insert his city and country including with his geo location (latitude and longitude and distance from other user). This will store in the database (Mongodb). Then when the user displace (change his place), automatically change his location information in the database. Already I created user and localization model.
Can anyone suggest me how to create an api to this idea?
If it is not clear, i will try to explain again.
Thanks
I’m not sure I understand what you mean by “localization” here. In web development, that usually means dealing with things like language in culture. It sounds like you are talking more about “location” or geographic location. They may be related, but not always - if I go to live in the Philippines for a month, I don’t want all my display changed to Tagalog. And of course, some locations may have more than one language. And don’t forget about VPNs. I think it would make more sense to let them change their location manually if they want.
In any case, I imagine that when a user creates an account, some location information is stored. And each time a user logs in, you can check their location and if it is different, hit an endpoint that updates the location in the database.
So are you asking how to update the database? Do you already have a database and an API?
@kevinSmith Thanks! I want to say “geo location”, sorry for that.
I have already created the database. I tried also by creating an api that update the recent geo location info in the DB, however I didn’t get any success when I receive a data from the front (react native) and that update the existing geo location info.
OK, you have the database. Do you have an API to store information the first time?
Do you have a repo so we can see what you’ve done?
Yes I have a register
function:
const registerUser = async (req, res, file) => {
/* Check if the user's phone number exists in the database */
const userExists = await User.findOne({ phone: req.body.phone });
if (userExists) {
res.status(400).json({ message: 'User already exists' });
} else {
try {
/* user not exists, so he/her will create his profile */
const createUser = function (user) {
return User.create(user).then((docUser) => {
console.log('\n>> Created User:\n', docUser);
return docUser;
});
};
const reqFiles = []
const url = req.protocol + '://' + req.get('host')
for (var i = 0; i < req.files.length; i++) {
reqFiles.push(url + '/upload/images/' + req.files[i].filename)
}
/* User insert his/her images in the database */
const createImage = function (userId, image) {
return Imageup.create(image).then((docImage) => {
console.log('\n>> Created Image:\n', docImage);
return User.findByIdAndUpdate(
userId,
{
$push: {
images: {
_id: docImage._id,
uploaded_Image: docImage.uploaded_Image,
},
},
},
{ new: true, useFindAndModify: false },
);
});
};
/* User insert his location */
const myLocation = (userId, location) => {
return Localization.create(location).then((docLocation) => {
console.log('\n>> My new Location:\n', docLocation);
return User.findByIdAndUpdate(
userId,
{
$push: {
localization: {
_id: docLocation._id,
city: docLocation.city,
country: docLocation.country,
postalCode: docLocation.postalCode,
geometry: docLocation.geometry,
}
}
},
{ new: true, useFindAndModify: false },
)
})
}
const run = async function () {
var user = await createUser({
firstname: req.body.firstname,
lastname: req.body.lastname,
age: req.body.age,
gender: req.body.gender,
phone: req.body.phone,
isAdmin: req.body.isAdmin,
hidden: req.body.hidden,
});
user = await createImage(user._id, {
uploaded_Image: reqFiles,
user: user,
});
user = await myLocation(user._id, {
city: req.body.city,
country: req.body.country,
postalCode: req.body.postalCode,
geometry: req.body.geometry,
user: user
})
console.log('\n>> User location:\n', user);
const newUser = await createUser(user);
console.log('new user user user: ', newUser);
newUser.save().then((data) => {
res.send(data);
});
};
run();
} catch (err) {
console.log(err);
}
}
};
And, that is working?
It works to insert! I couldn’t when user change his location
Right, I wouldn’t expect that to work for an update because:
if (userExists) {
res.status(400).json({ message: 'User already exists' });
Have you tried writing an endpoint to update location? Or just update everything to which you can send the id and whatever you want updated?
I tried by creating an endpoint to update the location by using a library called ipInfo
, and I updated successfully by getting an info from this library ipInfo
; however I didn’t like this library because it hasn’t full information. And also the update will come from the front (from mobile location (from react Native)), so I am looking for the best way if there is. Can you suggest me something if you have an experience on it?
Thanks for patience!
I tried by creating an endpoint to update the location by using a library called
ipInfo
,
OK, there are different issues here. You have a question of “How do I update a record in my API/DB?” That depends on what your setup is. We don’t know enough to do that. But it is going to be similar to creating one.
You are also asking, “How do I get ip info for a user?” That is a different question.
And also the update will come from the front (from mobile location (from react Native))
Naturally… we don’t care where the server is.
… so I am looking for the best way if there is.
Right, but there isn’t necessarily a “best” way - there are various ways that may or may not achieve your needs, with varying amounts of ease.
There are many libraries that can help you with this:
however I didn’t like this library because it hasn’t full information.
OK… but what do you mean by “full information”? And are you trying to get ip information or geolocation information? There is some overlap there but they aren’t exactly the same thing.
My instinct would be to do a search on npmjs.com and checkout what those packages give back and how that matches or doesn’t match my needs. When picking a library, I also like to try to get one with high weekly downloads and how well it is being maintained.
And are you trying to get ip information or geolocation information?
No, I am looking for a geolocation information. The library that I used searching a location with an ip. Just to test I was trying.
Yeah, sometimes you have to try a few libraries to find the one that you want. I provided a link for how to find some.
Thanks a lot your info!
I will try them.
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.