You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

向Back4App上传数据报错:crime_report.user_id需Pointer<_User>而非String

问题

在Back4App操作crime_report集合上传数据时,运行以下代码:

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);
}
}

每次运行都会触发报错:

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

已知错误原因是传入了字符串类型的用户ID,而集合要求user_id字段为Pointer<_User>类型,请问如何获取对应的指针?

解决方法

方式1:直接通过用户ID构造指针对象

无需查询数据库,直接用目标用户ID实例化Parse.User或Parse.Object('_User')对象,即可得到符合要求的指针:

// 构造User指针对象
const userPointer = new Parse.User();
userPointer.id = "jHfUJtvg2y";

// 替换原代码中的字符串赋值
crimeReported.set('user_id', userPointer);

也可以用Parse.Object的方式构造:

const userPointer = new Parse.Object('_User');
userPointer.id = "jHfUJtvg2y";
crimeReported.set('user_id', userPointer);

方式2:查询获取完整用户对象(适用于需要用户其他属性的场景)

如果后续需要使用该用户的其他数据,可以先查询用户对象,再将其赋值给user_id:

async function saveNewCrime(me,crime,date,location,description,city_of_crime) {
  const crimeReported = new Parse.Object('crime_report');
  crimeReported.set('crime_type', crime);
  crimeReported.set('date', date);
  
  // 查询指定ID的用户
  const query = new Parse.Query(Parse.User);
  try {
    const user = await query.get("jHfUJtvg2y");
    crimeReported.set('user_id', user);
    
    crimeReported.set('description', description);
    crimeReported.set('location',location);
    crimeReported.set('city',city_of_crime);
    
    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);
  }
}

注意事项

  • 确保传入的用户IDjHfUJtvg2y是_User集合中存在的有效ID,否则会触发“对象不存在”的错误。
  • 方式1性能更优,无需额外数据库查询;方式2适合需要同时操作用户数据的场景。

内容的提问来源于stack exchange,提问作者JerseyZ Cloutlord Rexdrey

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 03:20:30