Redux saga with firestore combination with react native - react-native

I am working in one project that using Redux saga with firestore and in a combination with react native.it is working fine but i have one issues with firestore response, because when i used yield call function its always return in promise and i used yield put to put data and its working fine with promise data but i need to use data with resolve value, and in this scenario put function call before resolve data. if i used delay than working fine but without delay not getting.
I am not much aware about redux saga so can any one help me out of this situation.
Here is code for reference, in this below code yield put call before data resolve and if i put yield delay() before yield put then working fine, but without delay function i have facing issues.
var resultData = [ ];
var resonseResult = yield call(Api.callWebservice) resonseResult.then(function(items){
console.log("resonseResult",item)
resultData = items
})
yield put({type: FETCH_DATA, resultData: resultData})

Related

Dynamic model attribute with SAILSJS

My use case needs me to use Dynamic Model attributes with my Sails.Js project and all of my methods fail. First I tried to call model1 method from model2 but it doesn't works. I also tried to use global variables but this fails too because, the global is set after the load of the application, and finaly I called model1Controller from the model2 but it still not working.
This is the mothod in my model2Controller
getbyTable:function(req,res){
let field = Field.find({fTable:req.param('tableName')});
field.exec(function(err, response) {
if(err) {
res.status(500);
return res.view('500', {data: err});
}
//res.json(response);
return response;
});
},
And this is my model2
'use strict';
const fieldController = require('../controllers/Model1Controller');
const donnees = fieldController.getbyTable('person');
module.exports={
}
When run it fires as error that req.param is not a function.
Need help!!! Thanks.
This looks like an interesting way to organize - I'd be curious to see how successful a project could be without Model attributes set in stone.
I can see a few specific errors in your code, maybe seeing them will help you move forward.
From your model2 you invoke getbyTable('person') with a string argument - but all controller methods are really intended to have reqest/response arguments (as in the definition getbyTable(req, res). Directly invoking a controller method is a bit unusual - if you ever did it, I'd expect to pass along a request and response from some other controller method.
Inside getbyTable, you try to return the response, but the response is only defined inside the exec callback. The response would be an array of objects fetched from your database - if that's what you need, you can't return them directly since database calls are async.
Your controller should probably do something with res in all logical branches, such as res.json, res.send, etc.
It's a bit hard to see exactly what you're trying to achieve, so maybe if you could explain the goal I could have some more relevant advice. For now, I could say that you might want to consider putting some code inside a service rather than a controller. Create file /api/services/MyService.js and use it to export a method that can be called from your model2 (you can require the service just like the controller). Keep the controller methods strictly for handling requests.
Good luck.

How to use module.exports and requireJS?

im quite a noob in html and js, so forgive me if this is a dumb question but, im trying to use requireJs to export modules in node and i can't get the function work right.
here is the code extracted from example.
first i have this main.js, as the note in the documentation says http://requirejs.org/docs/node.html#2
var sayHi = require(['./greetings.js'], function(){});
console.log(sayHi);
and a greetings.js who export the answer
module.exports= 'Hello';
});
and get nothing as result, so i define the exports and modules
define( function(exports,module){
module.exports= 'Hello';
});
and get as result:
function localRequire()
what am i doing wrong? i read the documentation and examples, but somehow i can't make this works.
I'm assuming the require call you are using is RequireJS's require call, not Node's require. (Otherwise, you'd get a very different result.)
You are using the asynchronous form of the require call. With the asynchronous form, there is no return value for you to use, you have to use the callback to get module values, like this:
require(['./greetings.js'], function(sayHi){
console.log(sayHi);
});
However, because you are running in Node, you can do this:
var sayHi = require('./greetings.js');
Note how the first argument is a string, not an array of dependencies. This is the synchronous form of the require call. The returned value is the module you required. When you are in Node, RequireJS allows you to call this synchronous form anywhere. When you are running the browser, it is only available inside a define call.

Chaining waterline calls with Promises

I have been hitting my head off a wall on this for the last 3 days.
I am using sailsjs & the waterline ORM that comes bundled. I want to run DB calls one after an other. I know I can do this by nesting inside "then" calls but it just looks wrong.
I have gone over the Q documentation and tutorials several times but I still don't get how to connect and fire "then" calls from existing Promises sequentially :(
I want to:
create a user
create a action
link the user & action
update the user
update the action
My code looks like
var mail = 'test#test.com';
Users.create({email:mail, name:''}).then(console.log).fail(console.log);
Actions.create({actionID:123})
.then(function(error, action){
Users.findOneByEmail(mail).then(function(person){
person.actions.add(action.id);
person.save(console.log);
}).fail(console.log)
});
Users.update({email:mail},{name:'Brian'}).exec(console.log);
Actions.update({actionID:123},{now:'running'}).exec(console.log);
As you can see from the code I've been using a mix of exec & then :P
I think the way is to connect the
Users.create(...).then -> Action.create(...).then -> Users.findOneByEmail(...).then -> *and the updates.
Huge thanks from any help
So after a day's research. I think I've cracked it.
Note: The first version I got working had the "then"s lined-up(removing the pyramid of doom) by returning the create. This allow me to call then on the next line to fire the create. http://documentup.com/kriskowal/q/#tutorial/chaining
Here's my final version
var mail = 'test#test.com';
Users.Create({email:mail,name:''})
.then(function(user){
return [Actions.create({actionID:123}),user];
}).spread(function(action, user){
user.action.add(action.id);
user.name = 'Brian';
user.save();
action.now = 'running';
action.save();
}).catch(console.error);
One of the cool things is the "spread" that allows you to line-up "Promises" and "values" to be return one they all have completed into the next "then".

durandal event not working properly

I am using durandal to pass messages between view models. So i used below code to send message
return (datacontext.getData("Test, testData))
.then(app.trigger('FireEvent', `dataObsArray`))
.fail(queryFailed);
Then i use below code to retrieve message
app.on('FireEvent').then(function (data) {
testObsArray(data);
});
But when i put breakpoint in the app.on on this line testObsArray(data);
it doesnt stop there. The debugger stops on line app.on('FireEvent').then(function (data)
I dont get data. Why is it so? When i pass data to dataObsArray , there are 10 records.
I am not sure why i am not getting any data. Where i am wrong? I am really new to Durandal so extremely sorry if i am not able to explain this properly and do let me know if you need more clarification.
Your problem is here;
.then(app.trigger('FireEvent', `dataObsArray`))
The way that will resolve is to call app.trigger, get the result, and pass that as the next step in the chain to then(), which is unlikely to be what you want. You need to wrap that in an anonymous function so that then() can call it after the dataContext call.
return (datacontext.getData("Test, testData))
.then(function(data) {
app.trigger('FireEvent', data?) //This depends on what getData returns
})
.fail(queryFailed);

Sencha Touch 2 store is loaded multiple time [Maximum call stack size exceeded]

I'm having a weird problem with the Sencha Touch 2 Store class. Here is what I have:
A simple view PollsList that defines a list view (with a store attribute set to Polls). I've included the required store as follow: requires:['Polls'],
A store class Polls with a model attribute set to Poll and a dummy data attribute,
A model class named Poll (the simplest possible),
An app.js file with the following launch methode:
var pollsListView = Ext.create('PollsList');
Ext.Viewport.add(pollsListView);
Ext.Viewport.setActiveItem(pollsListView);
I've also included the stores: ['Polls'] declaration in the app.js as required.
Now, the weird thing is when I access the PollsList view, the Poll store is being loaded indefinitely, till I got a the following error:
Uncaught RangeError: Maximum call stack size exceeded sencha-touch.js:598
And the stack seems to loop on the following calls:
Ext.ClassManager.instantiate sencha-touch.js:6378
(anonymous function) sencha-touch.js:3198
(anonymous function) app/store/Polls.js:4
Ext.apply.globalEval sencha-touch.js:598
Ext.apply.globalEval sencha-touch.js:599
Ext.apply.loadScriptFile sencha-touch.js:7673
Ext.apply.require sencha-touch.js:7831
Ext.apply.syncRequire sencha-touch.js:7695
(anonymous function)
Any idea?
Seems weird, done many a times, but did not went wrong.
Any ways, try removing the setActiveItem as there is only one view and it will take it as active.
Remove the require line from the code, as it seems that currently it is not required though.(Just because i want to make this sample running).
Now, try initializing the store, as
var store = Ext.create("YourStore") //
var listControl = Ext.craeate("YourList" ,{store : "aboveCreatedStore" , ..... });
listControl.setData( {name :'hello'} ); // hoping the model is having one String type field
Ext.ViewPort.add(listControl);
Thats it...
The above given code is not a complete solution(its like a patch), but it is to make you know what wrong you are doing in your current code.