I have created API,to get the pincode related area details. When I enter pincode it shows area details of that pincode , if that detail exist in my databse. But sometimes that pincode related details are not available in my database, so i need to call it by public API and also store that passed pincode and area details in my database. In Command prompt i am getting response,of PostOffice array. So the details which I am getting needs to be stored in Database. How can I do that?
I am able to store pincode which I am passing in API , and in other fields such as Name,Description etc. runturns as empty[] in database.
I am using mlab for it
Changed code: i am able to store pincode & i need to store array result,on cammand prompt displaying the body,includes the PostOffice array,in which having Name,Description,Branchtype...etc I need to store all this field.schema contains this fields.
router.get('/pincode/:pincode',function(req, res) {
Pincode.find({pincode : req.params.pincode},function(error,pincode ){
//when pincode not found in db
if(pincode.length == ''){
console.log("gfhg");
function searchAndSave(pincode, callback){
var request = require('request');
res.statusCode = 302;
request('http://postalpincode.in/api/pincode/'+pincode,function (error, res, body) {
if (res.statusCode === 200) {
var pincodeData = new Pincode({
pincode:pincode,
body:body.PostOffice
});
pincodeData.save(function (err,results) {
if (err) {
return callback(err);
}
else {
console.log(body);
return callback(null,pincodeData);
}
});
}else
//no record found
return callback(null,null);
}
});
}
searchAndSave(req.params.pincode,function(errInSearchAndSave,result){
if(errInSearchAndSave){
console.log("error : ", errInSearchAndSave);
}else{
//pincode result return from api and db
res.json(result);
}
})
}else{
//pincode found in db
res.json(pincode);
}
});
});
Your have to check if result found in db or not and create separate callback function for better clarification
//create a seprate function for search and store db
function searchAndSave(pincode, callback){
var request = require('request');
res.statusCode = 302;
request('http://postalpincode.in/api/pincode/'+pincode,function (error, res, body) {
if (res.statusCode === 200) {
var pincodeData = new Pincode({
pincode:pincode,
body:body.PostOffice.Name
});
pincodeData.save(function (err,results) {
if (err) {
return callback(err);
}
else {
console.log(body);
return callback(null,pincodeData);
}
});
}else{
//no recoord found
return callback(null,null);
}
}
router.get('/pincode/:pincode',function(req, res) {
Pincode.find({pincode : req.params.pincode},function(error,pincode ){
//when pincode not found in db
if(!pincode){
searchAndSave(req.params.pincode,function(errInSearchAndSave,result){
if(errInSearchAndSave){
console.log("error : ", errInSearchAndSave);
}else{
//pincode result return from api and db
res.json(result);
}
})
}else{
//pincode found in db
res.json(pincode);
}
});
});
Related
I have written a function like this
function create(data, res){
var history= new historyData({
owner: data
});
historyData.save(function(err, historyData) {
//To identify the error
if (err) {
console.log(err);
}
//If no error pass the team details to databsae
else {
return historyData.id;
}
});
}
and I'm trying to get the return value like this
var history = create("saddsa", res);
but it gives undefined error
I want to get the id of the created collection and pass it to another function?
Try to return a callback and get that value
function create(data, res, callback) {
var history = new historyData({
owner: data
});
historyData.save(function(err, historyData) {
//To identify the error
if (err) {
console.log(err);
callback(err)
}
//If no error pass the team details to databsae
else {
return callback(null,historyData.id);
}
});
}
// call your function like this
create("saddsa", res,function(err,result){
if(!err){
console.log(result)
}
});
replace
return historyData.id;
with
if(res) res(historyData.id);
and call
create("saddsa", function (id) { var history = id; });
I am creating a 'refresh data' function in Node and I cannot figure out where to place the callbacks and returns. The function continues to run. Below is a list of things the function should do. Could someone help out?
Check if a user has an api id in the local MongoDB
Call REST api with POST to receive token
Store token results in a MongoDB
Terminate function
./routes/index.js
router.post('/refresh', function(req, res) {
var refresh = require('../api/refresh');
refresh(req, function() { return console.log('Done'); });
});
../api/refresh.js
var callToken = require('./calltoken');
var User = require('../models/user'); // Mongoose Schema
module.exports = function(req, callback) {
User.findOne( {'username':req.body.username}, function(err, user) {
if(err) { console.log(err) }
if (user.api_id == 0) {
callToken.postToken(req.body.username, callback);
} else { // Do something else }
});
};
./calltoken.js
var request = require('request');
var Token = require('../models/token'); // Mongoose Schema
module.exports = {
postToken: function(user, callback) {
var send = {method:'POST', url:'address', formData:{name:user} };
request(send, function(err, res, body) {
if(err) { console.log(err) }
if (res.statusCode == 201) {
var newToken = new Token();
newToken.token = JSON.parse(body).access_token['token'];
newToken.save(function(err) {
if(err) { console.log(err) }
return callback();
});
}
});
}
};
I'm not an expert in Express but everywhere in you code in lines with if(err) { console.log(err) } you should stop execution (maybe of course not - up to you app) and return 400 or 500 to client. So it can be something like
if(err) {
console.log(err);
return callback(err); // NOTICE return here
}
On successful execution you should call return callback(null, result). Notice null as a first argument - it is according nodejs convention (error always goes as first argument).
I would like to save an object from one schema to the array in another one. What I have is User schema and Events schema. User schema has, for example, a property "joined" which is array. When making put request, meeting should be pushed to "joined" array.
At the moment I have this code on clientside:
<div ng-repeat="event in events">
...
<button ng-click="joinEvent(event)">Join</button>
</div>
$scope.joinEvent = function(event){
$http.put('/join', event)
.success(function (data) {
$scope.users = data;
})
.error(function (data) {
console.log('Error: ' + data);
})
};
and this this in express.js routes:
app.put('/join', function (req, res){
var user = req.user; // logged in user
var id = req.user._id;
var update = { $addToSet: {joined: req.event} };
User.findByIdAndUpdate(id, update, {upsert: true}, function (err, user) {
if (!err) {
console.log("joined");
//return all users from db
User.find(function(err, users) {
if (err)
res.send(err)
res.json(users);
});
} else {
if(err.name == 'ValidationError') {
res.statusCode = 400;
res.send({ error: 'Validation error' });
} else {
res.statusCode = 500;
res.send({ error: 'Server error' });
}
console.log('Internal error(%d): %s',res.statusCode,err.message);
}
});
});
After all, I see 'null' in user.joined. What's wrong with my approach?
The way you're trying to get the event name is wrong.
In your code, req.event == null . That's why you see null in 'user.joined'.
In the client - event property should look like this:
event = {eventName: "someEventName"}
In the server - you reach to event name like this:
var update = { $addToSet: {joined: req.body.eventName} };
Thanks to Yarden for pushing me into the right direction. I made it in other way by passing event id on clientside and making Event.findById query before looking for user. That worked.
I am trying to parse an object from a javascript (a blog post head and body) through a node.js server and on to save it in the mongoDB.
this is the parsing code:
function saveState( event ) {
var url = '';
var postTitle = headerField.innerHTML;
var article = contentField.innerHTML;
var post = {
title: postTitle,
article: article
};
var postID = document.querySelector('.save').getAttribute('id');
if(postID != "new"){
url += "?id=" + postID
}
var request = new XMLHttpRequest();
request.open("POST", "draft" + url, true);
request.setRequestHeader("Content-Type", "application/json");
request.send(post);
}
this is sent to this node server handler:
app.post('/draft', routes.saveDraft);
exports.saveDraft = function(req, res){
var id = url.parse(req.url, true).query.id;
var post = db.getPostByID(id);
if(id){
console.log('post id' + id);
db.savePost(id, req.body.head, req.body.article);
}
else{
db.newPost(req.body.head, req.body.article);
}
res.render('editDraft.hbs', post); //send the selected post
};
and then, sent to one of these DB functions:
exports.newPost = function (postTitle, article) {
new postCatalog({title:postTitle,
_id:1,
author:'temp',
AuthorID:2,
date:'2/3/12',
inShort:article.substring(0,100),
content:article ,
published:false
}).save(function (err, login) {
if (err) {
return console.log('error');
}
else {
console.log('Article saved');
}
});
}
exports.savePost = function (id, postTitle, article) {
postCatalog.find({_id: id}).save(function (err, login) {
if (err) {
return console.log('error');
}
else {
console.log('Draft saved');
}
});
}
now, I just can't get this to work..
I am new to node and I could really use your help!
thanks
EDITED:
the parameters being sent to the DB saving functions were not written properly.
but i'm still stuck in the same place, where the data is being sent but not saved correctly. I think there's something wrong with my getPostByID function but I can't figure it out:
exports.getPostByID =function (id) {
var post = postCatalog.find({_id: id}, function(err, post){
if(err) {
return handleError(err);
}
else{
if(post > 0){
post = post[0];
}
return post;
}
});
return post;
}
I am using express (including bodyparser) and mongoose. view engine is hbs.
thanks again.
You have to write it the asynchronous way, e.g. your getPostByID:
exports.getPostByID = function (id, callback) {
postCatalog.find({_id: id}, function(err, post) {
if (err) {
callback(err);
}
else if (post && post.length > 0) {
callback(null, post[0]);
}
else {
callback(null, null); // no record found
}
});
};
And this is true for your whole code. It's totally different and the way you tried it will never work under Node.js.
BTW there is a mongo-driver method findOne which is better suited in this special case but I didn't want to change your code too much.
I am trying to POST a request for /draft and create a new "draft" / update an existing one in my database. after that I want to instantly redirect to the /draft?id=RELEVANT_ID_HERE page.
this is my current POST request function:
app.post('/draft', function(req,res){
var id = req.query.id;
var postTitle = req.body.head;
var article = req.body.article;
if(id){
db.postCatalog.findOneAndUpdate({_id: id}, {title:postTitle, inShort:article.substring(0,100), content:article}, function(err, data){
if (err) {
return handleError(err);
}
else {
console.log(data);
res.status(200).redirect('/draft?id='+id);
}
});
}
else{
id = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
return v.toString(16);
});
new db.postCatalog({title:postTitle,
_id:id,
author:'temp',
AuthorID:2,
date:'2/3/12',
inShort:article.substring(0,100),
content:article ,
published:false
}).save(function (err, data) {
if (err) {
return handleError(err);
}
else {
console.log(data);
res.status(200).redirect('/draft?id='+id);
}
});
}
});
so, everything works except for the redirect. I am getting the correct GET request in the node console, but nothing happens in the browser.
this is the code for the GET request:
app.get('/draft', function(req,res){
var id = req.query.id;
if(id){
db.postCatalog.findOne({_id: id}, function(err, post){
if(err) {
return handleError(err);
}
else{
if(post){
console.log(post);
res.status(200).render('editDraft.hbs', {post: post});
}
else{
routes._404(req,res);
}
}
});
}
else{
console.log('creating new draft');
res.status(200).render('editDraft.hbs');
}
});
I am using Express and Mongoose. view engine is Handlebars.
Thanks for reading!
I think the status 200 is throwing you off. Try using a 302 and it should work.
res.writeHead(302, {
'Location': '/draft?id='+id
});
res.end();