On localhost this works fine, but I when I put it on the live server the put requests are not sticking to the database. Specifically with req.body I have to define the attribute I want to send {points: req.body.points}
update: function(req, res) {
console.log("PUT received: " + req.body.points);
models.Player.update({ _id: req.params.pid }, req.body, function(err, player) {
if (err) {
res.json({error: 'Player not found.'});
} else {
res.json(player);
}
});
},
The only way I can get the above to work on the live server is, if I write the object to update like below. This is not good because the app updates more than just points, it may send a put to update {player_name: req.body.player_name} you know what I mean.
I don't understand why req.body isn't working, it usually does!!
update: function(req, res) {
console.log("PUT received: " + req.body.points);
models.Player.update({ _id: req.params.pid }, {points: req.body.points}, function(err, player) {
if (err) {
res.json({error: 'Player not found.'});
} else {
res.json(player);
}
});
},
EDIT: My quick solution was to do this, but I would rather just use the req.body object, whats the reason it's not working?
update: function(req, res) {
var test = {
player_name : req.body.player_name,
player_picture : req.body.player_picture,
bench : req.body.bench,
points : req.body.points,
rebounds : req.body.rebounds,
steals : req.body.steals,
blocks : req.body.blocks,
fouls : req.body.fouls,
team_id : req.body.team_id,
}
models.Player.update({ _id: req.params.pid }, test, function(err, player) {
if (err) {
res.json({error: 'Player not found.'});
} else {
res.json(player);
}
});
},
Related
I developped an invoice using ReactJS, nodejs and mysql, it have a dynamic table, as you can see the invoive component in this link : https://codeshare.io/5zqLeN
My router is :
exports.ajouterfact = function(req, res) {
console.log("req", req.body);
var today = new Date();
var factures = {
"Nfact": req.body.Nfact,
"Nom": req.body.Nom,
"Datefact": req.body.Datefact,
"Note": req.body.Note,
"Nomp": req.body.Nomp,
"QuantiteF": req.body.QuantiteF,
}
connection.query('INSERT INTO factures SET ?', factures, function(error, results, fields) {
if (error) {
console.log("error ocurred", error);
res.send({
"code": 400,
"failed": "error ocurred"
})
}
else {
// console.log('The solution is: ', results);
res.send({
"code": 200,
"success": "facture registered sucessfully"
});
}
})
};
My invoice is :
When I submit it, It will be added on the database by getting:
req { Nfact: '15',
Nom: 'Imen',
Datefact: '2018-09-04T09:47:32.925Z',
Note: 'Test',
QuantiteF: '' }
The Noun of product (Nomp) is null and the quantity is ''.
But when I run my backend with Postman, it works well.
How can I fix that ?
Hello you beautiful people you.
I'm trying to create REST APIs using node.js connected to an OracleDB, but i'm pulling my hair out trying to get these stupid bind variables working.
Here's my code:
app.get('/mailsummary/:SCHEMA', function (req, res) {
"use strict";
oracledb.getConnection(connAttrs, function (err, connection) {
if (err) {
// Error connecting to DB
res.set('Content-Type', 'application/json');
res.status(500).send(JSON.stringify({
status: 500,
message: "Error connecting to DB",
detailed_message: err.message
}));
return;
}
connection.execute("select * from :SCHEMA.event#db3", [req.params.SCHEMA], {
outFormat: oracledb.OBJECT // Return the result as Object
}, function (err, result) {
if (err || result.rows.length < 1) {
res.set('Content-Type', 'application/json');
var status = err ? 500 : 404;
res.status(status).send(JSON.stringify({
status: status,
message: err ? "Error getting vendor mailing summary." : "Vendor or DB does nto exist.",
detailed_message: err ? err.message : ""
}));
} else {
res.contentType('application/json').status(200).send(JSON.stringify(result.rows));
}
// Release the connection
connection.release(
function (err) {
if (err) {
console.error(err.message);
} else {
console.log("GET /mailsummary/" + req.params.SCHEMA + " : Connection released");
}
});
});
});
});
For some reason i'm getting the error
OracleDB : ORA-01036: illegal variable name/number
If I remove the bind variable, assign a static value and remove "req.params.SCHEMA" after the sql statement and leave the brackets blank, it works.
connection.execute("select * from peeps.event#db3", [], {
outFormat: oracledb.OBJECT // Return the result as Object
I know it's got to be something simple with the way i'm pulling in the bind variable, but i'm pulling my hair out.
Please help me Obi-Wan Kenobi... you're my only hope.
Thanks!
Bind variables are placeholders used to transfer data between database and client program. You are trying to transfer the text of the SQL statement - your usage won't work.
This bind behavior is not specific to node-oracledb; it's the way Oracle works. It helps keep data and statement text separate.
There is some general bind info at: http://docs.oracle.com/database/122/LNOCI/binding-and-defining-in-oci.htm#GUID-77A26CEA-1C41-46A2-866C-622F9FEB5482
I'm trying to do a search quest using js-zimbra module, i keep on getting an error that i didn't knew how to fix,
her's my code :
var z = require('js-zimbra');
var comm = new z.Communication({
url: "myzimbraurl/service/soap"
});
// Authenticate
comm.auth({
"username": "user",
"secret": "pass",
"isPassword": true
}, function(err) {
if (err) {
// An error occured authenticating!
}
console.log('done !')
// Now, carry on creating requests and sending them
comm.getRequest({}, function(err, req) {
if (err) {
// Can't create a new request
}
req.addRequest({
name: "SearchRequest",
namespace: "zimbraMail",
params: {
"SearchRequest" : {
"types" : "message",
"fetch" : "all"
},
"query": {
"_content": "in:inbox"
}
}
}, function(err) {
if (err) {
console.log('err', err);
}
comm.send(req, function(err, res) {
if (err) {
console.log('err', err);
}
console.log(res);
})
})
})
});
and when i execute i got the following error message :
error: Received server error: {"Code":{"Value":"soap:Receiver"},"Reason":{"Text":"error while proxying request to target server: Connection reset"},"Detail":{"Error":{"Code":"service.PROXY_ERROR","Trace":"","_jsns":"urn:zimbra"}}}
Is there any fix for such error ?
Simple:
models.Message.find({ chat_id: req.params.chat_id }).skip(80).limit(30).sort({sent:1}).exec(function(err, message) {
if(err) {
res.json({error: 'Message not found.'});
} else {
res.json(message);
}
});
How can I write skip() to skip the entire collection minus the last 30, making it dynamic?
Detail:
The reason I need to do this is because it's a chat app, and the messages need to return oldest to newest hence sort({sent:1}) but the collection can get big and I only want to send 30 documents.
So for example
[
{sent: 1, message:'hey'},
{sent: 2, message:'sup'},
{sent: 3, message:'nttn much'}
]
For this example I want to send 1 message to the client, statically it could look like this.
models.Message.find({ chat_id: req.params.chat_id }).skip(2).limit(1).sort({sent:1}).exec(function(err, message) {
if(err) {
res.json({error: 'Message not found.'});
} else {
res.json(message);
}
});
That will return the latest {sent: 3, message:'nttn much'}, this is good.
BUT without the skip method it would look like this
models.Message.find({ chat_id: req.params.chat_id }).limit(1).sort({sent:1}).exec(function(err, message) {
if(err) {
res.json({error: 'Message not found.'});
} else {
res.json(message);
}
});
returning this to the client {sent: 1, message:'hey'}, not so good.
So obviously I need to skip, I need to do more scouring on the mongoDB docs, but there has to be a way to check the collection length then minus x or in this case about 30, then pass that in to skip()?
Any idea how I do that?
You can use skip(80).limit(30).sort({sent:-1}) to get last 30 documents, then in your client-side-app sort it with the way you wanted.
So I realized I can work with the object after querying the DB before doing res.json and sending to the client. Here is what I came up with, and seems to work as of now.
getById: function(req, res) {
if (req.user) {
models.Message.find({ chat_id: req.params.chat_id }).sort({sent:1}).exec(function(err, message) {
if(err) {
res.json({error: 'Message not found.'});
} else {
console.log(message.length);
message = message.slice(message.length - 30);
console.log(message);
res.json(message);
}
});
}
},
I'm very new to Nodejs and i'm trying to find a record in my db(mongo) by submitting a code and crosschecking the record to see if it exists.
I'm not really clear on how to approach this to get to an endresult stating "There is a code, please continue" and "There is no code".
This is what i have so far as a result of my fiddling:
userProvider.js:
UserProvider.prototype.findByCode = function(code, callback) {
this.getCollection(function(error, user_collection) {
user_collection.findOne({code: code}, function(error, result) {
callback(error, result)
});
});
}
The get collection belonging to userProvider.js:
UserProvider.prototype.getCollection= function(callback) {
this.db.collection('users', function(error, user_collection) {
if( error ) callback(error);
else callback(null, user_collection);
});
};
app.js:
app.post('/game/code', function(req, res) {
if (req.param('code')) {
userProvider.findByCode(req.param('code'), function(error, result) {
console.log(error);
console.log(result);
});
}
});
And the jade form submitting the code:
extends layout
block content
h1= "Gimme a code"
div
form( method="post")
div
div
span.label code :
input(type="text", name="code", id="code")
div
input(type="submit", value="Verbind")
Console logging the param('code') results in the code. So i'm pretty sure that one's solid. So why's there a null returned?
From the question, you didn't say where the 'null' was returned. I am new to nodejs as well, however I noticed in your code:
app.post('/game/code', function(req, res) {
if (req.param('code')) {
userProvider.findByCode(req.param('code'), function(error, result) {
console.log(error);
console.log(result);
});
}
});
and you didn't call the 'res' object. You should try something like this:
app.post('/game/code', function(req, res) {
if (req.param('code')) {
userProvider.findByCode(req.param('code'), function(error, result) {
if(error)
res.send(404, 'There is no code');
else {
console.log(result);
res.send('There is a code:'+result);
}
});
}
});
Check out http://expressjs.com/api.html#res.status for various things you do with res object