简体   繁体   中英

Failed to create new object: schema mismatch for crime_report.user_id; expected Pointer<_User> but got String

async function saveNewCrime(me,crime,date,location,description,city_of_crime) {
//Create your Parse Object
const crimeReported = new Parse.Object('crime_report');
//Define its attributes
// crimeReported.set('objectId', id);
crimeReported.set('crime_type', crime);
crimeReported.set('date', date);
crimeReported.set('user_id', "jHfUJtvg2y");
crimeReported.set('description', description);
crimeReported.set('location',location);
crimeReported.set('city',city_of_crime);

try {
  //Save the Object
  const result = await crimeReported.save();
  console.log('New object created with objectId: ' + result.id);
} catch (error) {
  console.log('Failed to create new object: ' + error.message);
}
}

I am trying to upload data to back4app and in a crime reported collections every time I try to run the code I get an error saying

Failed to create new object: schema mismatch for crime_report.user_id; expected Pointer<_User> but got String

from what I understand is that im passing a string where it expects a pointer does anyone know how I can approach getting a pointer?

Try the following code:

(async function saveNewCrime(me,crime,date,location,description,city_of_crime) {
//Create your Parse Object
const crimeReported = new Parse.Object('crime_report');
//Define its attributes
// crimeReported.set('objectId', id);
crimeReported.set('crime_type', crime);
crimeReported.set('date', date);
crimeReported.set('user_id', new Parse.User({'objectId': 'jHfUJtvg2y'}));
crimeReported.set('description', description);
crimeReported.set('location',location);
crimeReported.set('city',city_of_crime);

try {
  //Save the Object
  const result = await crimeReported.save();
  console.log('New object created with objectId: ' + result.id);
} catch (error) {
  console.log('Failed to create new object: ' + error.message);
}
})();

You got this error because you sent "jHfUJtvg2y" string.

  1. You can change user_id column type to string.

2. You can pass the user pointer to user_id field.

for example:

if you have a user class and user by this id.

crimeReported.set('user_id', (await new Parse.Query(Parse.User).get('jHfUJtvg2y')));

this code returned user object and this is pointer to parse user.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM