Why can't I pass an object to `navigation.navigate()`? - react-native-navigation

I'm using the UI Kitten library. I can store routes in an array and pass an array index to navigate()
Here's what I'm doing. I store my routes in an array like this:
const routeNames = [
'Customers',
'Employees',
]
And then I reference them on a navigation tab:
<BottomNavigation
selectedIndex={state.index}
onSelect={onSelect}
>
<BottomNavigationTabItem
title={routeNames[0]}
/>
...
And handle on select:
const onSelect = index => {
navigation.navigate(routeNames[index]);
};
But I would rather reference their name for better readability like this:
<BottomNavigationTabItem
title={routeNames.Customer}
/>
And therefore store the route names in an object:
const routeNames = {
CustomerList: 'Customers',
EmployeeList: 'Employees',
}
But doing it this way with the object gives me an error:
You need to specify name or key when calling navigate with an object
as the argument
I feel like it shouldn't matter as long as I pass a string for the route name.
UPDATE: It looks like the docs say a number type is required for onSelect ... but is the array index not referencing a string?
What do I not understand about how array references work?

Related

How to fill a reference field automatically in Wix (Velo), with or without code?

I am using a reference field, which contains users nickname, to make a connection between my main collection and 'public data' collection created as default by Wix. I created this reference field to populate a repeater from the two 'main' and 'public data' collections. Is it possible to automatically fill a reference field without using code? If not, then how can use 'beforeInsert' hook to fill the 'the reference' field using code.
I tried to do so in the backend by this code, but it doesn't work.
import { currentMember } from 'wix-members-backend';
export function Collection1_beforeInsert(item, context) {
currentMember.getMember()
.then((member) => {
const id = member._id;
const fullName = `${member.contactDetails.firstName} ${member.contactDetails.lastName}`;
const nickname = member.profile.nickname
console.log(nickname)
item.referenceField= "nickname"
// return member;
})
.catch((error) => {
console.error(error);
});
return item;
}
First off, I'm assuming you're using a regular reference field and not a multi reference field.
Second, it's important to understand how reference fields work before continuing. The reference field holds the ID of the item it is referencing. So when you say the reference field contains a user's nickname, that can't be true. It can contain the ID of the item in PrivateMembersData collection with the nickname you want.
Third, as just mentioned, the nickname field does not exist in the PublicData collection. It is part of the PrivateMembersData collection.
So, if you want to connect your collection to another with the nickname field, you need to set your reference field to reference the PrivateMembersData collection and then store the proper ID in that field.
(Side point: In your code you are putting the same string, "nickname", in every reference field value. You probably meant to use nickname without the quotes. You're also not using promises correctly.)
You can try this code. It should get you closer to what you're looking for.
import { currentMember } from 'wix-members-backend';
export async function Collection1_beforeInsert(item, context) {
const member = await currentMember.getMember();
item.referenceField = member._id;
return item;
}

Sequelize INSERT handling Object that has sometimes value of NULL as an attribute

I am trying to insert multiple objects to a Postgres database using Sequelize ORM. These objects are obtained from another project that I personally can't change or modify. These objects will have attributes that have object inside them.
Example object:
{
id:1,
name:'foo',
created:{user:{'insertattributehere'},date:'insertdatehere'},
modify:{user:{'insertattributehere'},date:'insertdatehere'},
}
For simplicity purposes, I have created a table that has each attribute of object as a column which will have a datatype of String(id as string, name as string, created_user_attribute, created_date etc).
So when inserting the object, I would simply the following INSERT.
const new_user = await ClassName.create({id: object.id, name: object.name, created_user_attribute: object.user.attribute ...})
However, sometimes, the attribute that contains another object can be null, for example
{
id:2,
name:'bar',
created:{date:'insertdatehere'}, notice that created doesnt have attribute User
modify:{user:{'insertattributehere'},date:'insertdatehere'},
}
This will result on TypeError, since 'created' doesn't have 'user' attribute. What I want is a method to somehow will handle this TypeError, and insert a NULL value (or "" for the string)
I could, as a last resort, manually check every attribute for a null value to handle the TypeError, and then create a nested statement such that I will insert a no value string instead. However, this looks very repetitive and inelegant.
Is there a way to handle this problem in a better way? Note that I can't change the objects that I want to insert to my database.
You can use lodash function omitBy along with the optional chaining operator to get only defined props and if omitted props has no default value in DB they will have null values by default:
const new_user = await ClassName.create(
_.omitBy({
id: object.id,
name: object.name,
created_user_attribute: object.user?.attribute
}, _.isUndefined)
)

Mongoose returning Array value as "undefined" [duplicate]

This question already has answers here:
Can't access object property of a Mongoose response
(4 answers)
Closed 2 years ago.
About ready to frisbee my laptop into the drywall here.
I have a Mongo document containing several different objects and arrays. The main ones of interest are the categories array of objects and the bypass array of Strings.
I'm using model.findOne() to obtain the document with success. The value of the document is stored in the res variable and console.log(res) shows the correct values without errors. I can even SEE the contents of the categories and bypass elements.
When I assign res.categories to a variable, no errors occur. Hooray, the basics of assignment are working.
When I assign res.bypass to a variable (immediately after the above assignment), the variable value is undefined even though I can see the contents of res.bypass containing an Array of Strings as designed.
The structure of res.bypass is bypass: [ '123', '456', '78', '90' ]. Assigning that value to a variable sets it as undefined.
Am I missing something here? Am I not allowed to take a single document and assign its different elements to different variables?
EDIT 1:
Code Snippet Request:
await Api.findOne({ id: '1' }).then(async (doc) => {
console.log(doc.categories) // Categories Array of Objects
console.log(doc.bypass) // Bypass Array of Strings
const categories = doc.categories // Value is categories Array of Objects
const bypass = doc.bypass // Value is undefined
...
})
There's no need to wrap the promise callback in an async function, nor to use a then handler on an awaited promise. You should change it to:
const doc = await Api.findOne({ id: '1' });
console.log(doc.categories) // Categories Array of Objects
console.log(doc.bypass) // Bypass Array of Strings
const categories = doc.categories // Value is categories Array of Objects
const bypass = doc.bypass // Value is undefined

Replace key-value pair in an object with Google Apps Script

I am making a call to an API for a commercial product using Apps Script. Unfortunately, the resulting object has several key-value pairs that contain the id from a linked table.
I can easily get the standard values and have written code to find the related name value and add it to the object. I would prefer to add the name in the same location as the original id. But, when I add a new key, it is added to the end of the object.
I want the name in the same location as id so when I insert it into a sheet, the columns will still be in order.
This is my object:
var json = {id: 4730183,name: "A A", customer_source_id:123, company: "NE Company"};
This is my desired object after replacing the id with the name:
var json = {id: 4730183,name: "A A", source:"CRM", company: "NE Company"};
Basically, I want to find customer_source_id in the object and replace it with source.
I can't use indexOf and splice because the object is not an array.
What is the best way to do this? Do I have to convert it to an array first and then back again?
A quick answer would be:
var obj = {id: 4730183,name: "A A", customer_source_id:123, company: "NE Company"};
var json = JSON.stringify(obj);
json = json.replace("customer_source_id","source")
The better answer is:
#Waqar Ahmed is correct. JavaScript objects are unordered. In your example "var json" is an object not JSON. You can make it JSON with JSON.stringify(json). But once the JSON is parsed into an object it again becomes unordered. You should not use it to store ordered data.
I am not sure if it is efficient, but you can iterate through the keys and build a new json object like this:
var newjson = {};
for(var key in json){
if(key === 'customer_source_id'){
newjson.source = [NEW VALUE TO DISPLAY];
}else{
newjson[key] = json[key];
}
}
json = newjson;
But, like #Waqar and #Spencer said, the object is not used for ordered data.
You can do his only in java script array. Not in JSON. JSON is meant to be addressed by keys, not by index.Change your json to
var json ={id: 4730183,name: "A A", customer_source_id:null, items : [] company: "ESI"};
Now you can insert items using push method of array.
json.items.push('My Item');

Mongoose: what's up with "_doc"?

It seems Mongoose is doing something really funky internally.
var Foo = new mongoose.model('Foo', new mongoose.Schema({a: String, b: Number}));
var foo = new Foo({a: 'test'; b: 42});
var obj = {c: 1};
foo.goo = obj; // simple object assignment. obj should be
// passed by reference to foo.goo. recall goo
// is not defined in the Foo model schema
console.log(foo.goo === obj); // comparison directly after the assignment
// => false, doesn't behave like normal JS object
Essentially, any time you try to deal with properties of a Mongoose model that aren't
a) defined in the model's schema or
b) defined as the same type (array, obj, ..) ... the model doesn't even behave like a normal Javascript object.
Switching line 4 to foo._doc.goo = obj makes the console output true.
edit: trying to reproduce weirdness
example 1:
// Customer has a property 'name', but no property 'text'
// I do this because I need to transform my data slightly before sending it
// to client.
models.Customer.find({}, function(err, data) {
for (var i=0, len=data.length; i<len; ++i) {
data[i] = data[i]._doc; // if I don't do this, returned data
// has no 'text' property
data[i].text = data[i].name;
}
res.json({success: err, response:data});
});
_doc exist on the mongoose object.
Because mongooseModel.findOne returns the model itself, the model has structure (protected fields).
When you try to print the object with console.log it gives you only the data from the database, because console.log will print the object public fields.
If you try something like JSON.stringify then you get to see inside the mongoose model object. (_doc, state ...)
In the case where you want to add more fields in the object and it's not working
const car = model.findOne({_id:'1'})
car.someNewProp = true // this will not work
If later you set the property to the object car and you didn't specify in the Model Schema before then Mongoose model is validating if this field exists and if it's the valid type.
If the validation fails then the property will not be set.
Update
Maybe I misunderstood your original question, but now it looks like the nature of your question changed, so the below information isn't relevant, but I'm leaving it. :)
I tested your code and it works fine for me. Mongoose doesn't execute any special code when you set properties that aren't part of the schema (or a few other special properties). JavaScript currently doesn't support calling code for properties that don't yet exist (so Mongoose can't get in the way of the set of the goo property for example).
So, when you set the property:
foo.goo = { c: 1 };
Mongoose isn't involved. If your console.log was something other than the code you displayed, I could see that it might report incorrectly.
Additionally, when you send the results back as JSON, JSON.stringify is being called, which calls toString on your Mongoose Model. When that happens, Mongoose only uses the properties defined on the schema. So, no additional properties are being sent back by default. You've changed the nature of the data array though to directly point at the Mongoose data, so it avoids that problem.
Details about normal behavior
When you set the property goo using Mongoose, quite a few things happen. Mongoose creates property getters/setters via the Object.defineProperty (some docs). So, when you set the goo property, which you've defined as a [String], a few things happen:
Mongoose code is called prior to the value being set onto the object instance (unlike a simple JavaScript object)
Mongoose creates an array (optionally) to store the data (a MongooseArray) which will contain the array data. In the example you provided, since you didn't pass an array, it will be created.
Mongoose will attempt to cast your data to the right type
It will call toString on the data passed as part of the cast.
So, the results are that the document now contains an array with a toString version of the object you passed.
If you checked the contents of the goo property, you'd see that it's now an array with a single element, which is a string that contains [object Object]. If you'd picked a more basic type or matched the destination property storage type, you would see that a basic equality check would have worked.
you can use toJSON() instead of _doc
Try using lean
By default, Mongoose queries return an instance of the Mongoose Document class. Documents are much heavier than vanilla JavaScript objects, because they have a lot of internal state for change tracking. Enabling the lean option tells Mongoose to skip instantiating a full Mongoose document and just give you the POJO.
https://mongoosejs.com/docs/tutorials/lean.html
Had same problem. Instead of updating my model.
const car = model.findOne({_id:'1'})
let temp = JSON.stringify(car);
let objCar = JSON.parse(temp);
objCar.color = 'Red'; //now add any property you want
this solves my problem
I was stuck on this today... Drove me nuts. Not sure if the below is a good solution (and OP has mentioned it too), but this is how I overcame this issue.
My car object:
cars = [{"make" : "Toyota"}, {"make" : "Kia"}];
Action:
console.log("1. Cars before the color: " + car);
cars.forEach(function(car){
car.colour = "Black"; //color is NOT defined in the model.
});
console.log("2. Cars after the color: " + car);
Problematic console output:
1. Cars before the color: [{"make" : "Toyota"}, {"make" : "Kia"}];
2. Cars after the color: [{"make" : "Toyota"}, {"make" : "Kia"}]; //No change! No new colour properties :(
If you try to pass in this property that was undefined in the model, via doc (e.g. car._doc.color = "black"), it will work (this colour property will be assigned to each car), but you can't seem to access it via EJS (frontend) for some reason.
Solution:
(Again, not sure if this is the best way... but it worked for me): Add in this new property (colour) in the car model.
var carSchema = mongoose.Schema({
make: String,
color: String //New property.
})
With the model redefined, everything worked as normal / expected (no _doc 'hacks' needed etc.) and I lived another day; hope it helps someone else.
There is some weirdness with Mongoose models and you have to check that Mongoose doesn't already have a model created in it's models array.
Here is my solution:
import mongoose from 'mongoose';
createModel = (modelName="foo", schemaDef, schemaOptions = {})=> {
const { Schema } = mongoose;
const schema = Schema(schemaDef, schemaOptions);
const Model = mongoose.models[modelName] || mongoose.model(modelName, schema);
return Model;
}
I use my own mongoose model class and base class for my models. I made this and it should work for you.
For those using spread(...) and/ can't see a solution, here's an example of #entesar's answer
Instead of spread or ._doc in:
import User from "./models/user";
...
async function createUser(req, res) {
const user = await User.create(req.body);
res.status(201).json({
message: "user created",
data: {
...user // OR user._doc,
token: "xxxxxxxx",
},
});
}
...
Use this
import User from "./models/user";
...
async function createUser(req, res) {
const user = await User.create(req.body);
res.status(201).json({
message: "user created",
data: {
...user.toJSON(),
token: "xxxxxxxx",
},
});
}
...
Ps: took me a while to understand the answer.
You should add .lean() on the find to have it skip all the Model "magic".

Resources