【问题标题】:Update One Record to add object Error(NodeJS)更新一条记录以添加对象错误(NodeJS)
【发布时间】:2021-12-18 06:26:36
【问题描述】:

我在玩我的 nodjs 和 mongoDB。我发现无法更新对象的错误。

const { MongoClient, ObjectID } = require('mongodb');
// or as an es module:
// import { MongoClient } from 'mongodb'

const url =
  'mongodb+srv://<user>:<password>@cluster0.uw6pe.mongodb.net/myFirstDatabase?retryWrites=true&w=majority';
console.log(url);
const client = new MongoClient(url);

// Database Name
const dbName = 'studio';

export async function insertData(_id: string) {
  // Use connect method to connect to the server
  await client.connect();
  console.log('Connected successfully to server');
  const db = client.db(dbName);
  const collection = db.collection('shops');

  return new Promise(function (resolve, reject) {
    collection.updateOne(
      { _id: ObjectID(_id) },
      {
        $set: {
          address: {
            type: 'Point',
            coordinates: [123, 321],
          },
        },
      },
      (err, res) => {
        if (err) reject(err);
        resolve(res);
      },
    );
  });
}

我试图通过使用 NestJS 插入地址(对象)。并得到了这个错误。

enter image description here

所以我想知道我做错了什么。我可以更新键:值,但我不能更新键:对象, 如果我输入一个空对象,它会起作用。

有人有答案吗?

【问题讨论】:

    标签: javascript node.js mongodb nestjs


    【解决方案1】:

    问题是coordinates: [123, 321] 不是有效的坐标,这是因为321 不是有效的纬度值。纬度在 -90 到 +90 之间,而经度可以在 -180 到 +180 之间。

    线索在错误信息中:"longitude/latitude out of bounds"

    如果您确保您的坐标有效,则不应遇到此错误。

    有用的阅读文档:

    【讨论】:

    • 谢谢。我不明白那部分。
    猜你喜欢
    • 1970-01-01
    • 2021-04-12
    • 2019-02-18
    • 2014-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-06
    相关资源
    最近更新 更多