Giter Site home page Giter Site logo

geoPoint validation about gstore-node HOT 9 CLOSED

lostpebble avatar lostpebble commented on May 25, 2024
geoPoint validation

from gstore-node.

Comments (9)

sebelga avatar sebelga commented on May 25, 2024 1

I finally found the time to test your data. I spot the error, the regex I was using to validate is not correct. I will release a fix this week-end. thanks for reporting!

from gstore-node.

sebelga avatar sebelga commented on May 25, 2024

You're right gstore could validate and convert to geoPoint such object. I will add it to the next release. Thanks for reporting!

from gstore-node.

sebelga avatar sebelga commented on May 25, 2024

Hi, just released v0.9.0 with this feature. Could you please test and tell me if it does validate correctly? thanks!

from gstore-node.

lostpebble avatar lostpebble commented on May 25, 2024

As per the other issue, this seems to be resolved for the global gstore.save() method. But for the regular Entity.save() method I'm still getting a validation error.

from gstore-node.

sebelga avatar sebelga commented on May 25, 2024

That's strange. Could you please share the "schema" and "data" you try to save so I can try it?

from gstore-node.

lostpebble avatar lostpebble commented on May 25, 2024

Sure, here's the schema I'm trying to write to:

const schema = new gstore.Schema({
  country: {
    type: 'string',
  },
  localityUrlMapping: {
    type: 'string',
  },
  location: {
    type: 'geoPoint',
  },
  placeName: {
    type: 'string',
    excludeFromIndexes: true,
  },
  subLocalityUrlMapping: {
    type: 'string',
  },
  sublocality: {
    type: 'boolean',
  },
  sublocalityParentId: {
    type: 'string',
  },
  updatedDate: {
    type: 'datetime',
    default: gstore.defaultValues.NOW,
  },
});

const TestLocalityUrlMapping = gstore.model('TestLocalityUrlMapping', schema);

export default TestLocalityUrlMapping;

I'll show you the code that works first:

I'm trying to transfer data over from an entity called LocalityUrlMapping to TestLocalityUrlMapping - they shared exactly the same schema structure, as above. Its a little strange, but I wanted to test out re-saving entities before I ran it on my real data.

const query = LocalityUrlMapping.query().limit(50);

const result = await query.run({ readAll: true, format: gstore.Queries.formats.JSON });
const response = result[0];

const resaveEntities = [];

for (let i = 0; i < response.entities.length; i += 1) {
    const data = Object.assign({}, response.entities[i]);
    delete data.id;

    const entity = new TestLocalityUrlMapping(data, response.entities[i].id);

    resaveEntities.push(entity);
}

await gstore.save(resaveEntities);

So, that works fine. But this does not:

const query = LocalityUrlMapping.query().limit(50);

const result = await query.run({ readAll: true, format: gstore.Queries.formats.JSON });
const response = result[0];

const resaveEntities = [];

for (let i = 0; i < response.entities.length; i += 1) {
    const data = Object.assign({}, response.entities[i]);
    delete data.id;

    const entity = new TestLocalityUrlMapping(data, response.entities[i].id);

    resaveEntities.push(entity.save());
}

await Promise.all(resaveEntities);

Basically, what should be happening here is that every single Promise in the resaveEntities array should be run and resolved- but I get a validation error before it gets anywhere, like this:

(node:5876) Warning: a promise was rejected with a non-error: [object Error]
(node:5876) Warning: a promise was rejected with a non-error: [object Error]
(node:5876) Warning: a promise was rejected with a non-error: [object Error]
Unhandled rejection ValidationError: [object Object]
    at ValidationError.GstoreError (d:\Dev\_Projects\Vibescout\vibescout-web-v3\node_modules\gstore-node\lib\error.js:9:26)
    at ValidationError (d:\Dev\_Projects\Vibescout\vibescout-web-v3\node_modules\gstore-node\lib\error.js:29:13)
    at Object.keys.forEach (d:\Dev\_Projects\Vibescout\vibescout-web-v3\node_modules\gstore-node\lib\model.js:953:37)
    at Array.forEach (native)
    at ModelInstance.validate (d:\Dev\_Projects\Vibescout\vibescout-web-v3\node_modules\gstore-node\lib\model.js:873:38)
    at Promise (d:\Dev\_Projects\Vibescout\vibescout-web-v3\node_modules\gstore-node\lib\schema.js:62:23)
    at ModelInstance.fn (d:\Dev\_Projects\Vibescout\vibescout-web-v3\node_modules\gstore-node\lib\schema.js:60:20)
    at ModelInstance.next (d:\Dev\_Projects\Vibescout\vibescout-web-v3\node_modules\promised-hooks\hooks.js:109:44)
    at Promise (d:\Dev\_Projects\Vibescout\vibescout-web-v3\node_modules\promised-hooks\hooks.js:116:29)
    at ModelInstance.wrapper [as save] (d:\Dev\_Projects\Vibescout\vibescout-web-v3\node_modules\promised-hooks\hooks.js:71:20)
    at _callee$ (d:/Dev/_Projects/Vibescout/vibescout-web-v3/src/server/gcloud/datastore/interactions/reindexing/AnyEntity.js:41:32)
    at tryCatch (d:\Dev\_Projects\Vibescout\vibescout-web-v3\node_modules\regenerator-runtime\runtime.js:64:40)
    at GeneratorFunctionPrototype.invoke [as _invoke] (d:\Dev\_Projects\Vibescout\vibescout-web-v3\node_modules\regenerator-ru[...]

I have tried this out with only one entity too and its the same.

This is what the data variable looks like within the loop above. As you can see location is just a regular object before saving.
data-json

from gstore-node.

sebelga avatar sebelga commented on May 25, 2024

Mmm not seeing the error. Could you try to just save 1 entity with the data you have here? Also add a catch on the Promise (or inside a try catch with await) to get the exact error returned.

Note: you don't have to specify format: gstore.Queries.formats.JSON, it's the default value

from gstore-node.

lostpebble avatar lostpebble commented on May 25, 2024

Okay cool, I tried with just one entity returned from a query now.

This is the test function I'm running:

export async function testDatastoreSave() {
  const query = TestLocalityUrlMapping.query().limit(1);

  const result = await query.run({ readAll: true });

  const response = result[0];

  const data = Object.assign({}, response.entities[0]);
  const id = response.entities[0].id;

  delete data.id;

  log.json(data);

  const entityToSave = new TestLocalityUrlMapping(data, id);

  try {
    await entityToSave.save();
  } catch (e) {
    log.error(e.stack);
  }

  log.info("Completed saving");
}

For log.json(data) I get (prettified JSON):

sublocality:           false
location: 
  longitude: 174.76333618164062
  latitude:  -36.84846115112305
placeName:             Auckland
localityUrlMapping:    auckland
updatedDate:           Mon Dec 12 2016 13:13:35 GMT+0200 (South Africa Standard Time)
subLocalityUrlMapping: null
sublocalityParentId:   null
country:               NZ

And for the log.error(e.stack):

error: ValidationError: [object Object]
    at ValidationError.GstoreError (d:\Dev\_Projects\Vibescout\vibescout-web-v3\node_modules\gstore-node\lib\error.js:9:26)
    at ValidationError (d:\Dev\_Projects\Vibescout\vibescout-web-v3\node_modules\gstore-node\lib\error.js:29:13)
    at Object.keys.forEach (d:\Dev\_Projects\Vibescout\vibescout-web-v3\node_modules\gstore-node\lib\model.js:953:37)
    at Array.forEach (native)
    at ModelInstance.validate (d:\Dev\_Projects\Vibescout\vibescout-web-v3\node_modules\gstore-node\lib\model.js:873:38)
    at Promise (d:\Dev\_Projects\Vibescout\vibescout-web-v3\node_modules\gstore-node\lib\schema.js:62:23)
    at ModelInstance.fn (d:\Dev\_Projects\Vibescout\vibescout-web-v3\node_modules\gstore-node\lib\schema.js:60:20)
    at ModelInstance.next (d:\Dev\_Projects\Vibescout\vibescout-web-v3\node_modules\promised-hooks\hooks.js:109:44)
    at Promise (d:\Dev\_Projects\Vibescout\vibescout-web-v3\node_modules\promised-hooks\hooks.js:116:29)
    at ModelInstance.wrapper [as save] (d:\Dev\_Projects\Vibescout\vibescout-web-v3\node_modules\promised-hooks\hooks.js:71:20)
    at _callee$ (d:/Dev/_Projects/Vibescout/vibescout-web-v3/src/server/gcloud/datastore/interactions/testing/testingMethods.js:23:24)
    at tryCatch (d:\Dev\_Projects\Vibescout\vibescout-web-v3\node_modules\regenerator-runtime\runtime.js:64:40)
    at GeneratorFunctionPrototype.invoke [as _invoke] (d:\Dev\_Projects\Vibescout\vibescout-web-v3\node_modules\regenerator-run[...]

from gstore-node.

sebelga avatar sebelga commented on May 25, 2024

Actually I was interested in the error object itself to see the message :) not so much the stack.

But could you try, just to save your data (no query before).
Something like:

const data = {
    sublocality: false,
    location: {
        longitude: 174.76333618164062,
        latitude:  -36.84846115112305,
    },
    placeName:  "Auckland",
    localityUrlMapping: "auckland",
    updatedDate:  new Date(), // you might have a problem here in the validation with your date from the query
    subLocalityUrlMapping: null,
    sublocalityParentId:   null,
    country: "NZ"
}

const entityToSave = new TestLocalityUrlMapping(data);

try {
    await entityToSave.save();
} catch (e) {
   log.error(e.stack);
}

from gstore-node.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.