Unable to insert array data into mongodb using mongoose - mongodb

I am trying to insert data into my mongodb database. I am using mern stack.
This is my user model for the city data that is in array containing city_name & city_code
city: [{
city_name: {type: String},
city_code: {type: String},
}],
I am passing the values to the register function in this manner
city: [{city_name: this.state.c_name, city_code: this.state.c_code}],
This is the function defined to register into the database
city: [{ city_name: req.body.c_name, city_code: req.body.c_code}],
No error's message is being returned in the console. I am using a message where if the user is registered successfully it returns User Registered or else the error message. But I am not getting anything in the console.
Constructor defined in the front end side to get the values
city: [{city_name: '', city_code: ''}],
UPDATED
This is the function I am using to post the data
export const register = newUser => {
return axios
.post('users/sign-up', {
username: newUser.username,
email: newUser.email,
phone: newUser.phone,
dob: newUser.dob,
city: [{city_name: newUser.c_name, city_code: newUser.c_code}],
password: newUser.password
})
.then(response => {
console.log('Registered')
})
}

SOLUTION
So whenever you are accessing the data stored in array we need to give the index value.
<td>{this.props.obj.city[0].cityname}</td>
<td>{this.props.obj.city[0].citycode}</td>

Related

TypeError: parent.child.push is not a function - issue pushing my object to database

Problem
I'm trying to send data from my client-side form into my database and am running into the error below:
TypeError: artist.fans.push is not a function
The data is an email address that should be saved into my artist model as a subdoc (in an object).
What I've tried
I'm using the syntax from the mongo docs that says parent.children.push() is the proper way to add subdocs to arrays and parent.children.create() to create new subdocuments. Both yield the same error.
Here's my function:
module.exports.addFanEmail = async (req, res) => {
const fan = req.body;
const artist = await Artist.findById(req.params.id);
artist.fans.create(fan);
await artist.save();
res.redirect(`/artists/${artist._id}`);
}
Right now req.body is only the "fan's" email - here's an example of the object's format: { email: 'tom#test.com' }
DB Model
const artistSchema = new Schema({
image: [ImageSchema],
genre: [ String ],
fans: {
email: String,
subscribed: String,
gender: String,
age: Number
},
});
The object is coming through from the client to the function without any problem, I just can't get it to save to the db?

Cannot set the reference to the user document in courses mongoose

I am currently working on a RESTful API, and I am trying to reference the users schema in the courses document such that, when a POST request gets sent to the route of the course, a course is created in the DB and has as one of its fields a reference to the user that created it. However, for the life of me, I cannot figure out why the "user" field is not appearing when I post. There seem to be quite a few of these questions here on Stack so I may just be adding to the pile, but I tried their solutions and they did not work for me
var mongoose = require('mongoose')
var Schema = mongoose.Schema
var userSchema = new Schema({
firstName: {
type: String,
required: true
},
lastName: {
type: String,
required: true
},
emailAddress: {
type: String,
required: true
},
password: {
type: String,
required: true
}
});
var CourseSchema = new Schema({
user: {type: Schema.Types.ObjectId, ref: 'User'}, //FOR some reason this is not showing up on any courses created using the
title: {
type: String,
required: true
},
description: {
type: String,
required: true
},
estimatedTime: {
type: String
},
materialsNeeded: {
type: String
}
});
var User = mongoose.model('User', userSchema);
var Course = mongoose.model('Course', CourseSchema);
module.exports = {Course, User};
Do you see anything in here that would preclude the user field from appearing when a new course is created?
I have attached some screenshots to further explain.
This first image is a screen of the currently authenticated user credentials (fake data obviously). This is the user that is sending the POST request for the new course. I would expect his information to be attached to the course (see screenshot 3)
This image shows the body of the request that is sent. You can see that the key-value pairs match what is in the CourseSchema. I would expect that the "user" field would be created once the POST request is sent.
This last image is some dummy data that is the expected result.
Thanks all for taking a look at this!
User field will not be automatically added to the course document. You have to manually set the user field in the request body itself or while creating a course.
Example of the course body to be sent:-
{
user: "userId",
title: "test",
description: "test",
estimatedTime: "test",
materialsNeeded: 1
}
Also, the result of this will not include the whole user document as you have mentioned in the expected result. It will only return the userId. However, while accessing the course you can populate the user field to get the whole user document. Example for the same
Course.find({...query}).populate("user")

Correctly inserting and/or updating many datasets to MongoDB (using mongoose)?

So from time to time I get new exports of a cities database of POIs and info about them and I want to have all that data in my MongoDB with a Loopback-API on it. Therefore I reduce the data to my desired structure and try to import it.
For the first time I receive such an export, I can simply insert the data with insertMany().
When I get a new export, it means that it includes updated POIs which I actually want my existing POIs to be replaced with that new data. So I thought I'd use updateMany() but I could'nt figure out how I'd do that in my case.
Here's what I have so far:
const fs = require('fs');
const mongoose = require('mongoose');
const data = JSON.parse(fs.readFileSync('data.json', 'utf8'));
// Connect to database
mongoose.connect('mongodb://localhost/test', {
useMongoClient: true
}, (err) => {
if (err) console.log('Error', err);
});
// Define schema
let poiSchema = new mongoose.Schema({
_id: Number,
name: String,
geo: String,
street: String,
housenumber: String,
phone: String,
website: String,
email: String,
category: String
});
// Set model
let poi = mongoose.model('poi', poiSchema);
// Generate specified data from given export
let reducedData = data['ogr:FeatureCollection']['gml:featureMember'].reduce((endData, iteratedItem) => {
endData = endData.length > 0 ? endData : [];
endData.push({
_id: iteratedItem['service']['fieldX'],
name: iteratedItem['service']['fieldX'],
geo: iteratedItem['service']['fieldX']['fieldY']['fieldZ'],
street: iteratedItem['service']['fieldX'],
housenumber: iteratedItem['service']['fieldX'],
phone: iteratedItem['service']['fieldX'],
website: iteratedItem['service']['fieldX'],
email: iteratedItem['service']['fieldX'],
category: iteratedItem['service']['fieldX']
});
return endData;
}, []);
//
// HERE: ?!?!? Insert/update reduced data in MongoDB collection ?!?!?
//
mongoose.disconnect();
So I just want to update everything that has changed.
Of course if I leave it to insertMany() it fails due to dup key.
For the second time, use mongo's update command with upsert set to true.
db.collection.update(query, update, options)
In the query pass the _id ,in update pass the object and in option set upsert to true. This will update the document if it exists creates a new document if that doesn't exist.

Associated Data Not Returned From Apollo to Client?

Here's relevant Apollo code. (Many database fields have been omitted to make it faster/easier to read this post.)
CONNECTORS
const userAccountsDataModel = db.define('userAccountsData', {
id: {type: Sequelize.STRING, primaryKey: true},
picture_medium: {type: Sequelize.STRING}
});
const associatesDataModel = db.define('associatesData', {
_id: {type: Sequelize.STRING, primaryKey: true},
first_name: {type: Sequelize.STRING},
last_name: {type: Sequelize.STRING},
userID: {type: Sequelize.STRING},
});
associatesDataModel.belongsTo(userAccountsDataModel, {foreignKey: 'userID'});
SCHEMA
type userAccountsData {
id: String
picture_medium: String
}
type associatesData {
_id: String
first_name: String
last_name: String
userID: String
userAccountsData: [userAccountsData]
}
QUERY
const GETONEASSOCIATE_QUERY = gql`
query getOneAssociate($_id: String!) {
getOneAssociate(_id: $_id) {
_id
first_name
last_name
userID
accountsData{
id
picture_medium
}
}
} `;
RESOLVER
getOneAssociate(_, args) {
console.log('in getOneAssociate');
var ret = connectors.associatesData.findAll({
where: args,
include: [connectors.userAccountsData],
logging: console.log
}).then((res) => res.map((item) => item.dataValues));
return ret;
},
Via Sequelize, the resolver generates the following SQL:
SELECT "associatesData"."_id",
"associatesData"."first_name",
"associatesData"."last_name",
"associatesData"."userID",
"userAccountsDatum"."id" AS "userAccountsDatum.id",
"userAccountsDatum"."picture_medium" AS "userAccountsDatum.picture_medium"
FROM "associatesDatas" AS "associatesData"
LEFT OUTER JOIN "userAccountsData" AS "userAccountsDatum"
ON "associatesData"."userID" = "userAccountsDatum"."id"
WHERE "associatesData"."_id" = '35';
When I run the above SQL in my SQL client, I can see that all the data sought is being returned, including the data from the related "userAccountsData" table.
However, when the data gets to my client code running in javascript the browser, the data from the related table is missing-- the object that should contain it is null.
What am I missing?
Thanks in advance to all for any info!
This turned out to be an anomaly with Sequelize's automatic pluralizing of table names.
By default, Sequelize automatically pluralizes table names. It is supposed to be possible to turn this behavior off by using Sequelize's freezeTableName property. However, per this Sequelize issue post, there are cases in which Sequelize ignores the freezeTableName property and pluralizes (and sometimes singularizes) the table names anyway.
After discovering this I was able to resolve the anomaly by pluralizing the relevant table name and going through all my Apollo code to update the references to this table so that they would be pluralized as well.

When editing an existing document in React, Meteor Simple Schema validation on client side always returns 'true', despite invalid form data

I have a form component in React that can be used to either save a new address, or edit an existing address object in MongoDB. I am using SimpleSchema for client-side schema validation (as well as server-side schema validation).
The document is being passed as a prop to the form with shippingAddress:
<ShippingAddressForm
onChange={this.handleAddressChange.bind(this)}
shippingAddress={shippingAddress}
editModeType={editModeType}
/>
In case of New Address mode type, shippingAddress is passed as a skeleton object:
shippingAddress = {
firstName: "",
address: "",
city: "",
zipCode: "",
company: "",
lastName: "",
address2: "",
state: "",
addressTypeSelected: "",
country: "US",
phoneNumber: ""
}
In case of Edit Address, shippingAddress is used to pass corresponding values from existing MongoDB document:
shippingAddress = {
address: "Arcadia Bay Ct"
city: "Delray Beach"
company: "AT&T"
country: "US"
createdAt: Tue Aug 23 2016 11:59:13 GMT+0530 (IST)
favorite: false
firstName: "Max"
label: "Arcadia Bay Ct"
lastName: "Pricefield"
phoneNumber: "987654321"
state: "FL"
userId: "WEwmG3iYgShasmzGT"
zipCode: "33446"
}
Then the ShippingAddressForm inputs are populated using these values.
Before submitting the form, I validate the formData using a React handler. (This is being done via a submit button in a higher-level component, to which any state changes are regularly propagated.)
onSubmitHandler() {
// get address from form
// check it's complete and send error messages
// if successful save and return to address book
const { userId, saveShippingAddress } = this.props;
let formData = this.state.shippingAddress
let invalidKeys,
validityContext = AddressSchema.namedContext("shippingAddress");
formData.userId = userId;
formData.label = formData.address;
AddressSchema.clean(formData);
if(!validityContext.validate(formData)) {
invalidKeys = validityContext.invalidKeys();
invalidKeys = _.map(invalidKeys, function (o) {
return _.extend({message: validityContext.keyErrorMessage(o.name)}, o);
});
return toastr.error(invalidKeys[0].message, 'Error');
}
return saveShippingAddress(formData);
}
The validation in the above function works perfectly in case of a New Address. If any required key is missing, validityContext.validate(formData) returns false as it should.
However, in case of Edit Address, validityContext.validate(formData) always returns true, even if the data is invalid and required keys are missing etc.
I have checked the formData object being passed into the .validate() function, it is fine. It is not passing the old database object. If my form is missing a 'Last Name' field entry, then formData object is also missing a lastName key. But the SimpleSchema validator still returns true in edit mode.
I have also tried checking just one key using .validateOne(). Same issue occurs there.
The same invalid data is then being caught in server-side validation check and error is returned. However, I need the client-side validation to work in all cases as well.
I am not using MongoDB modifiers.
Has anyone else faced this issue?