InitialState in RTK gives undefined - redux-toolkit

Here is my CartSlice
import { createSlice } from "#reduxjs/toolkit";
//const cartItems =[] ;
const cartSlice = createSlice({
name:'cart',
initialState:{
cartItem:[],
quantity:1
},
reducers:{
add_to_cart: (state,action) => {
//state.value = action.payload.quantity;
//state.cartItem.push(action.payload.item);
//state.cartItem = action.payload.item;
//return {cartItem:action.payload.item};
state.cartItem.push(action.payload.item);
//console.log('CS',state.value);
//console.log('CIL',state);
}
}
});
export default cartSlice.reducer;
export const {add_to_cart} = cartSlice.actions
the error says this
Uncaught TypeError: Cannot read properties of undefined (reading 'push')
Can someone please help i can't get around this ...

Related

MongoDB Trigger Function // TypeError: Cannot access member 'id' of undefined

I'm trying to crate a file when a User will the first time be authenticated. I use the down below function. Somehow I am getting the following error: "TypeError: Cannot access member 'id' of undefined". I don't know why I can't access the user.id.
exports = function(authEvent){
const mongodb = context.services.get("mongodb-atlas");
const users = mongodb.db('data').collection('User');
const { user } = authEvent
const newObjcId = new BSON.ObjectId()
const newUser = {
_id: newObjcId,
realm_id: user.id
};
return users.updateOne({_id: newObjcId}, newUser, {upsert: true});
};

Error: Cannot find module 'react-bootstrap/lib/Breadcrumb'

I am following this tutorial: https://youtu.be/YPbgjPPC1d0 at 34:55 I am trying to use "truffle test", but keep getting Error: Cannot find module 'react-bootstrap/lib/Breadcrumb'
My code:
const { should, assert } = require('chai')
const { useReducer } = require('react')
const { Item } = require('react-bootstrap/lib/Breadcrumb')
const color = artifacts.require('./color.sol')
require('Chair')
useReducer(require('chai-as-promised'))
should()
contract ('color', (accounts) => {
describe('deployment', async () => {
It('deploys successfully', async () => {
contract = await color.deployed()
const address = contract.address
console.log(address)
assert.notEqual(address, '')
})
})
})
I am using Virtual Studio Code
Has anyone had similar problem or know how to fix it? Thanks

ChangeStream#destroy is not a method?

I got this error:
app:tail-mongodb ERROR Unhandled rejection:
z.cs.destroy is not a function
TypeError: z.cs.destroy is not a function
at createChangeStream
(/Users/Olegzandr/codes/interos/read-from-mongodb/dist/main.js:74:18)
at process.internalTickCallback (internal/process/next_tick.js:77:7)
here is my code:
import {ChangeStream} from "mongodb";
const z = {
cs: null as ChangeStream,
};
const createChangeStream = () => {
if (z.cs) {
z.cs.removeAllListeners();
z.cs.destroy();
}
const changeStream = z.cs = d.db(dbName).collection(dbCollName).watch();
changeStream.on('change', next => {
if (!(next && next.fullDocument)) {
log.warn('changed doc did not have fullDocument property, or was undefined:', next);
}
bun.handleIn(next.fullDocument || next);
});
};
seems weird that destroy() is not a valid method on the ChangeStream. Does anyone know the proper way to close a ChangeStream? The TypeScript typings say that this method is available, but I guess it is not.

object is probably 'undefined' - Mocha

I am using Protractor. The below solution works, but i get this warning:
this.currentTest.state
- error TS2532: Object is possibly 'undefined'
(property) Mocha.Context.currentTest?: Mocha.Test | undefined
How do i fix this warning?
Test file:
const helper = new HelperClass();
afterEach(async ()=> {
const state = this.currentTest.state;
await helper.getSource(state);
});
Class File
import { browser, } from 'protractor';
export class HelperClass {
public getSource(state:any) {
if (state === 'failed') {
browser.driver.getPageSource().then(function (res) {
console.log(res);
});
}
}
}
I think the error occurs because the access to this.currentTest.state happens inside another function: the arrow function passed in to afterEach--flow analysis does not cross function boundaries. Try simply pulling that line outside of the function:
const helper = new HelperClass();
afterEach(async ()=> {
const state = this.!currentTest.state;
await helper.getSource(state);
});
Does that change anything?

Getting error when trying to use firestore.runTransaction from react-native-firebase

I followed the examples from https://rnfirebase.io/docs/v4.1.x/firestore/transactions and https://blog.invertase.io/getting-started-with-cloud-firestore-on-react-native-b338fb6525b9 but neither of these variations seem to work:
import rnFirebase, {firestore} from 'react-native-firebase'
rnFirebase.firestore().runTransaction(async tx => {
// nothing yet
} // undefined is not an object (evaluating 'ref[method].apply')
firestore.runTransaction(async tx => {
// todo
} // undefined is not an object (evaluating '_reactNativeFirebase.firestore.runTransaction')
firestore().runTransaction(async tx => {
// doesn't matter
} // ... is not a function ... is undefined
Here's the full code I am using...
Initializing the redux store:
// ./redux/index.js
import {createStore, applyMiddleware, combineReducers, compose} from 'redux'
import {composeWithDevTools} from 'redux-devtools-extension'
import thunkMiddleware from 'redux-thunk'
import RNFirebase from 'react-native-firebase'
import {firebaseReducer, reactReduxFirebase, getFirebase} from 'react-redux-firebase'
import {reduxFirestore, firestoreReducer, getFirestore} from 'redux-firestore'
import lists from './modules/lists'
// Firebase config
const firebaseConfig = {
apiKey: "XXX",
authDomain: "XXX",
databaseURL: "XXX",
projectId: "xxx",
storageBucket: "xxx.appspot.com",
messagingSenderId: "xxx"
}
// react-redux-firebase options
const rrfConfig = {
userProfile: 'users', // firebase root where user profiles are stored
enableLogging: false, // enable/disable Firebase's database logging
attachAuthIsReady: true, // attaches auth is ready promise to store
// workaround for https://github.com/invertase/react-native-firebase/issues/431
enableRedirectHandling: false,
allowMultipleListeners: true,
}
const reactNativeFirebaseConfig = {
debug: true
}
export default (initialState = {}) => {
// initialize firebase
const firebase = RNFirebase.initializeApp(reactNativeFirebaseConfig)
// Initialize Cloud Firestore through Firebase
RNFirebase.firestore()
const reducer = combineReducers({
lists,
firebase: firebaseReducer,
firestore: firestoreReducer,
})
const middleware = [
// make getFirebase available in third argument of thunks
thunkMiddleware.withExtraArgument({getFirebase, getFirestore}),
]
return createStore(
reducer,
initialState,
compose(
reactReduxFirebase(firebase, rrfConfig), // pass initialized react-native-firebase app instance
reduxFirestore(firebase),
composeWithDevTools(applyMiddleware(...middleware))
)
)
}
And my action creator looks like
// ./redux/modules/lists.js
import RNFirebase, {firestore} from 'react-native-firebase'
// ...
// Async Action Creator
export const addToList = (attraction, listKey) => {
return async (dispatch, getState, {getFirestore}) => {
const reduxFirestore = getFirestore() // provided by redux-firestore... this gives the same behavior as using react-native-firebase directly...
try {
const ref = `lists/${listKey}`
const res = await reduxFirestore.get(ref)
const {attractionsIndex} = res.data() // this works fine
RNFirebase.firestore().runTransaction(async tx => {
const doc = await tx.get(ref) // line 139... this explodes
if (doc.exists) {
console.warn('exists!')
} else {
console.warn('does not exist!')
}
})
} catch(err) {
console.error(err)
}
}
}
, which results in the error
undefined is not an object (evaluating 'ref[method].apply')
wrapInDispatch
actions.js:57:21
_callee3$
lists.js:139:36
tryCatch
runtime.js:62:44
invoke
runtime.js:296:30
tryCatch
runtime.js:62:44
invoke
runtime.js:152:28
<unknown>
runtime.js:162:19
tryCallOne
core.js:37:14
<unknown>
core.js:123:25
_callTimer
JSTimers.js:154:6
_callImmediatesPass
JSTimers.js:202:17
callImmediates
JSTimers.js:470:11
__callImmediates
MessageQueue.js:329:4
<unknown>
MessageQueue.js:147:6
__guardSafe
MessageQueue.js:316:6
flushedQueue
MessageQueue.js:146:17