Unresolved function or method done(), spring with react - spring

Im currently following this https://spring.io/guides/tutorials/react-and-spring-data-rest/ tutorial. But when I write my app.js I get "Unresolved function or method done().
I have tried the other suggestion cleaning cache and add Node.js as global.
What am I missing?
const client = require('./client');
componentDidMount() {
client({method: 'GET', path: '/api/employees'}).done(response => {
this.setState({coaches: response.entity._embedded.employees});
});
}
**Client file **
'use strict';
var rest = require('rest');
var defaultRequest = require('rest/interceptor/defaultRequest');
var mime = require('rest/interceptor/mime');
var uriTemplateInterceptor = require('./api/uriTemplateInterceptor');
var errorCode = require('rest/interceptor/errorCode');
var baseRegistry = require('rest/mime/registry');
var registry = baseRegistry.child();
registry.register('text/uri-list', require('./api/uriListConverter'));
registry.register('application/hal+json', require('rest/mime/type/application/hal'));
module.exports = rest
.wrap(mime, {registry: registry})
.wrap(uriTemplateInterceptor)
.wrap(errorCode)
.wrap(defaultRequest, {headers: {'Accept': 'application/hal+json'}});

The best way is to do this tutorial following the tutor's code. The main trouble is that it is not actual or the author doesn't upgrade it because of his own motives. Anyway, if you look at the package.json you will the version of react in dependecies. So just change the react version
"react": "^15.3.2",
"react-dom": "^15.3.2"
github. Package.json

Related

react-native unable to resolve module 'events'

I have created an empty project with the following command:
react-native init demoProject
after installing
npm install -g react-native-cli
then I have installed mongoDB with the following command:
npm install mongodb --save
In the package.json file I have the following:
"dependencies": {
"events": "^1.1.1",
"mongodb": "^2.2.31",
"react": "16.0.0-alpha.12",
"react-native": "0.47.1"
},
In the index.ios.js file I am trying to import the mongodb as the following:
var MongoClient = require('mongodb').MongoClient;
After building the project in Xcode I have this error:
bundling failed: "Unable to resolve module `events` from `/Users/rzilahi/work/demo01/node_modules/mongodb/lib/apm.js`: Module does not exist in the module map
what am i doing wrong?
Why are you importing MongoClient to index.ios.js ? It shouldn't be imported in the index or any other file of your app. Database queries should be done in a webservice. So in your app, you can use fetch to retrieve data from a webservice and render that with a FlatList or component of choice. You can create a NodeJS project on port 1337 (example port) and run the Node Server. In that Node Project you need to import MongoClient and query the DB for data and use res.send or res.json to send the data back as a response. So if you're able to access your webservice at http://localhost:1337/getdata , you're all set.
You may need to use your actual IP address of the computer in fetch, like:
fetchData(){
fetch('http://192.168.0.4:1337/getdata')
.then((response) => response.json())
.then((responseJson) => {
this.setState({data: responseJson})
})
.catch((error) => {
alert(error);
});
}
And your node server would look something like:
var express = require('express'),
bodyParser = require('body-parser'),
mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/mydb');
var db = mongoose.connection;
mongoose.Promise = global.Promise;
var app = express();
//create the Posts Schema or import it.
app.get("/getdata", function(req, res){
Posts.find({}, function(e,doc){
if(doc==null){
res.send("NO POSTS");
}
else{
res.json(doc);
}
});
});
app.set('port', (process.env.PORT || 1337));
app.listen(app.get('port'), function(){
console.log("Server started on port: " + app.get('port'));
});

Stripe Payments with parse server cloud code

I have this code included in my main.js:
var stripe = require("/cloud/stripe.js")("sk_test_*********");
//create customer
Parse.Cloud.define('createCustomer', function (req, res) {
stripe.customers.create({
description: req.params.fullName,
source: req.params.token
//email: req.params.email
}, function (err, customer) {
// asynchronously called
res.error("someting went wrong with creating a customer");
});
});
After pushing this code to my Heroku server the logs indicate that: Error: Cannot find module '/cloud/stripe.js'
I have also tried var stripe = require("stripe")("sk_test_*********"); but this returns the same error. Whenever I try add this new module to my server the whole server becomes dysfunctional. What workarounds are there to this? Thanks
Have you added Stripe to the requirements of your package.json file for your node project? If so, you should be able to reference it using the term require('stripe') as opposed to what you're doing.
I'll tell you what worked for me, I racked my brain on this for a day. Instead of using Cloud Code to make a charge, create a route on index.js. Something like this in index.js
var stripe = require('stripe')('sk_test_****');
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({
extended: false
}));
app.post('/charge', function(req, res){
var token = req.body.token;
var amount = req.body.amount;
stripe.charges.create({
amount: amount,
currency: 'usd',
source: token,
}, function(err, charge){
if(err)
// Error check
else
res.send('Payment successful!');
}
});
I call this using jQuery post but you could also use a form.

How can I access the RethinkDB context set up by the Hapi plugin?

According to this Hapi JS plugin's documentation, The RethinkDB library and the connection are bound to the server context inside handlers on this.rethinkdb and this.rethinkdbConn.
However, I'm not being able to access those, only using request.server.plugins etc. Does anyone know why? Is it because the context set inside a plugin with server.bind() is only available inside the plugin itself ? Thanks!
EDIT:
Here's is some of the code on index.js
const plugins = [{register: require('hapi-rethinkdb'), options: {url: //url here}]
server.register(plugins, () => {
server.route(require('./routes'))
server.start()
})
The on routes.js, let's say a have a route like this:
{
method: 'GET',
path: '/list-of-things',
handler: function(request, reply) {
// I have to use it like this:
const r = request.server.plugins['hapi-rethinkdb'].rethinkdb;
const connection = request.server.plugins['hapi-rethinkdb'].connection;
// Because this throws undefined:
const r = this.rethinkdb;
const connection = this.rethinkdbConn
}
}

Node.js Express mongoose query find

I have a little problem with Express and mongoose using Node.js . I pasted the code in pastebin, for a better visibility.
Here is the app.js: http://pastebin.com/FRAFzvjR
Here is the routes/index.js: http://pastebin.com/gDgBXSy6
Since the db.js isn't big, I post it here:
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
module.exports = function () {
mongoose.connect('mongodb://localhost/test',
function(err) {
if (err) { throw err; }
}
);
};
var User = new Schema({
username: {type: String, index: { unique: true }},
mdp: String
});
module.exports = mongoose.model('User', User);
As you can see, I used the console.log to debug my app, and I found that, in routes/index.js, only the a appeared. That's weird, it's as if the script stopped (or continue without any response) when
userModel.findOne({username: req.body.username}, function(err, data)
is tried.
Any idea?
You never connect to your database. Your connect method is within the db.export, but that is never called as a function from your app.
Also, you are overwriting your module.exports - if you want multiple functions/classes to be exported, you must add them as different properties of the module.export object. ie.:
module.export.truthy = function() { return true; }
module.export.falsy = function() { return false; }
When you then require that module, you must call the function (trueFalse.truthy();) in order to get the value. Since you never execute the function to connect to your database, you are not recieveing any data.
A couple of things real quick.
Make sure you're on the latest mongoose (2.5.3). Update your package.json and run npm update.
Try doing a console.log(augments) before your if (err). It's possible that an error is happening.
Are you sure you're really connecting to the database? Try explicitly connecting at the top of your file (just for testing) mongoose.connect('mongodb://localhost/my_database');
I'll update if I get any other ideas.

NodeJS unable to response.write to the browser when there is delay in callbacks

i'm using simple MVC structure by Nathan Broslawsky. i have these code below.
ArticleProviderDBController.prototype.Show = function(data) {
//Init Model
var res = this.Response;
var model = this.getModel();
var view = this.getView("ArticleProviderDB");
model.findAll(function(error, article_collections){
if( error ) console.log(error);
view.renderGH(res, data, article_collections); //this will actually call the renderGH function to serve a html file with data from DB but it is not working.
res.write('inside callback'); //this will not.
//res.end();
});
//console.log(_self.Response);
res.write('outside callback'); //this will be shown on my browser.
//res.end();
}
actually i try to follow what people have done using expressjs
app.get('/', function(req, res){
articleProvider.findAll( function(error,docs){
res.render('index.jade', {
locals: {
title: 'Blog',
articles:docs
}
});
})
});
but seems like it is not working.
i also saw a post NodeJS response.write not working within callback posted recently but his solution is not working for me. My main objective is to use simple MVC structure created with Nodejs without the use of other templates such as expressjs to serve html with DB query. thank you.

Resources