【发布时间】:2022-06-22 12:02:40
【问题描述】:
const mongoose = require('mongoose');
main().catch(err => console.log(err));
async function main() {
await mongoose.connect('mongodb://localhost:27017/lakshKart');
}
const kittySchema = new mongoose.Schema({
name: String
});
kittySchema.methods.speak = function speak() {
const greeting = "Meow name is " + this.name;
console.log(greeting);
};
const shittyKart = mongoose.model('kittyKart', kittySchema);
const helloKitty = new shittyKart({ name: 'helloKitty' });
await kittyKart.save();
使用保存功能时出现错误等待只能在异步功能中使用 不知道怎么解决,求大神帮助。
【问题讨论】:
-
上面的代码调用
kittyKart.save,但我相信你想要的对象是shittyKart或helloKitty -
试过了,但它给出了相同的 SyntaxError: await 仅在异步函数和模块的顶层主体中有效
-
澄清一下,您尝试了
await helloKitty.save(),但它给了您这个错误?
标签: node.js mongodb mongoose async-await mongoose-web-server